Tilt UniversalCamera but keep keysDown moving in X direction

If I setup a universal camera with the keyboard inputs shown below and just point it horizontally it moves as I would like, that is only in the (forward, backward, left and right) horizontal directions. However, when I point the camera at any other beta angle, then the plane that the keysUp and keysDown keys move the camera in becomes relative to the way the camera is facing (I’ve tried camera.cameraDirection, camera.cameraRotation and camera.rotation but they all do the same thing). That is, the vertical height of the camera changes too. Is there a way to apply a beta angle (tilt) to the camera but for the keysUp/Down keys to only move the camera in the forward/backward horizontal directions?

camera.inputs.attached.keyboard.keysDown[0] = 40
camera.inputs.attached.keyboard.keysDownward[0] = 0
camera.inputs.attached.keyboard.keysUp[0] = 38
camera.inputs.attached.keyboard.keysUpward[0] = 0
camera.inputs.attached.keyboard.keysRotateRight[0] = 32

https://playground.babylonjs.com/#4MECEV

Is this example anything like what you need

Customizing Camera Inputs | Babylon.js Documentation ?

I played with that Playground example a lot earlier, but I think its over complicated for what I need. I won’t have a character moving around, I just need the camera to move horizontally and not vertically (I may have a separate button deal with the cameras vertical position, but I don’t want the arrows to do this).

For a simple solution you could try just setting cameraDirection.y to 0 each frame. That seems to work… :slight_smile:

At first it looked like that was working, but when I place some stuff much further in the z direction and move towards it, I can see that the y continues to reduce (albeit much less than before). Playground below to demonstrate.
https://playground.babylonjs.com/#PZ3GJU#1

Is there a way to reset the y on each render? (as my y will be a known value)

@Blake, your suggestion gave me an idea and I got it working, I used:

scene.registerBeforeRender(() => {
camera._position._y = 5
});

Fixed BabylonJS Playground:
https://playground.babylonjs.com/#PZ3GJU#2

1 Like

A note of caution re line 14, properties starting with an underscore are private and although unlikely to be changed it is not guarenteed that they will always work.

Does your solution not work if you use

camera.position.y = 5

?

If not try

camera.position.y = 5;
camera.computeWorldMatrix(true)
1 Like

Thanks @JohnK, that also works, so ultimate solution is:
https://playground.babylonjs.com/#PZ3GJU#3

1 Like