Moving camera horizontally

I am trying to move the camera horizontally for FPS player. With the default camera movement, when the camera is facing downwards, the horizontal movement stops.
I disabled the default movements and tried incrementing the position of the camera. I was able to achieve what I wanted, however, the collisions were no longer functional. I’ve attached some videos.

Default Camera Movement:

Incrementing camera position:

I can use imposters, but I don’t want to as it’d impact performance.

Is it possible to achieve the result shown in second video but with fixed collisions?

2 Likes

Could you repro the behavior you currently have in a playground ? so that the community could jump in and help

2 Likes

It sounds like you want to implement a custom camera controller, similar to the one demoed in this Walk Around playground:

https://playground.babylonjs.com/#CTCSWQ#1

Note the important lines 188 to 201, where the maths is applied to the camera’s movement. When forward is pressed, the camera is moved forward at a constant speed, no matter where the player is looking.

I’m completely new to Babylon and I’m currently implementing my own FPS camera; could anyone recommend tutorials / guidance that teach how to…

  1. Stop the camera from flying upwards when you look up and press forward?
  2. Allow the player to walk up and down slopes?
  3. Apply gravity? (constant speed is fine)
  4. Add a simple jump mechanic?

I think I’ve just answered my own question for number 1. Any tutorials that cover these basics greatly appreciated.

1 Like

I know @syntheticmagus has implemented all the mechanics you mentioned, maybe he can share some tips :slight_smile:

It looks like the playground you linked already has gravity implemented. A simple way to add a jump mechanic is to listen for the jump key, test if there’s a collision directly beneath the player, and if so apply some instantaneous upwards velocity. You can play with the amount of gravity and the up velocity until you get the feel you want- low gravity and high jump speeds can make it feel more arcade/platformer-like, while high gravity is more realistic.

2 Likes

@BlackThunder4007 & @jhancock532

We’ve got some stuff cooking on this, so please don’t take too hard a dependency on this, but I implemented a physics-enabled FPS controller as a proof of concept a few months back, if you want to look at that.

Input encapsulation experiment | Babylon.js Playground (babylonjs.com)

This approach includes gravity, jumping, sprinting, and interaction with both static and dynamic physics objects: you should be able to run into the big block, walk up/down the ramp, and knock over the stack of small blocks and push them around. Again, this is not final, so please take it with a grain of salt and know that we’ll be using it as a basis for some of the stuff we’re cooking up. :wink: But if you want to jump into how an FPS camera works, this might at least be interesting to look at. Hope this helps, and best of luck!

2 Likes

After searching through some playground examples and a bunch of questions on the forum. I was able to get the required movement using Vector3.addInPlace

camera.cameraDirection.addInPlace(
	new BABYLON.Vector3(
		Math.sin(camera.rotation.y),
		0,
		Math.cos(camera.rotation.y)
	).scale(speed)
);
1 Like

Thank you! Looking forward to seeing what you are developing, I’ll implement a basic system for now as this is an informal project. The example is very useful :slight_smile: