Camera rotation left/right on arrows

Hello,

I want to control camera rotation with an arrows (left/right). I have attached default controls to WASD

camera.keysUp.push(87); // “w”
camera.keysDown.push(83); // “s”
camera.keysLeft.push(65); // “a”
camera.keysRight.push(68); // “d”

I want to disable default control on arrowLeft and ArrowRight and switch it with camera rotations. But leave WSAD functionalities.

Thanks

1 Like

You should try to empty the array before :slight_smile: push or affect a new one.

1 Like

That was helpful. Thank you very much.

Follow up question. I’ve this part of code now to listen for arrow inputs

canvas.addEventListener(“keydown”, (e)=>{
//if left arrow is pressed
if(e.keyCode == 37) {
camera.cameraRotation.y -= 0.05;
}
//if right arrow is pressed
else if(e.keyCode == 39) {
camera.cameraRotation.y += 0.05;
}
})

This works, but while holding the keydown for a continuous rotation, that rotation is not very smooth (which is expected behavior with this type of implementation I guess). Is there a way to smooth things out?

You should take the time between frames in accounts an debounce the events. Basically only turn once per frame as there is no reason to do more and use engine.deltaTime to know how much you should turn to simulate a constant rotation speed. With vector the function would be a lerp and a slerp for quaternion interpolation.

Thank you @sebevan. This helped me a lot. I appreciate it.

This is an old topic but I got confused reading it so I figured I’d leave a note. I also wanted to have WASD to move the cam and arrows to rotate it. Switching move controls to WASD is as shown in the initial post, as for adding rotation control via arrow keys, have a look at the FreeCameraKeyboardRotateInput as detailed in Customizing Camera Inputs | Babylon.js Documentation. But do not remove the FreeCameraKeyboardMoveInput.