Move forward in universal camera with right-click

I would like to move forward with the UniversalCamera/FreeCamera by holding down the right mouse button. How can I do this?

I also switched off oncontextmenu on the canvas and added the following to the camera: camera.keysUp.push(3);

Hey @bestog,

As far as I know, the keysUp property will only work with specifying specific keys on the keyboard so I don’t think you can use that to map forward movement to the right-click. I suspect you may have to make a custom input for the Camera using something like a FreeCameraInputsManager. You can learn more about this here. You might be able to just add an EventListener to the canvas for the “pointerdown” event and use the which properties to determine if the right mouse button is clicked:

canvas.addEventListener("pointerdown", function(e){
        if(e.which == 2 || e.which == 3)
        // Do something
    });

While this may not be the best course of action, it could be a good place to start. It should be noted for the code above that the right mouse button will register as a 2 or 3, depending on the browser that you use.

If I can put together a better solution, I’ll make sure to let you know.

1 Like

So here’s an implementation that is using the event listener: Babylon.js Playground

I used two functions for tracking when the right mouse button was pressed to work as a semaphore (sort of). Basically, when you press the button, the bool is true and runs a basic Lerp function to move the camera. When it’s released, the movement loop stops updating the camera position.

I’m not sure if this is ultimately what you’re looking for but if not, feel free to elaborate on what you’re looking for.

(Also thanks to @syntheticmagus for helping me out with this)

3 Likes