Get camera movement direction and speed

Hi, I was wondering if it is possible to get the camera’s movement direction and speed in the renderloop.

I’m trying to get a static gun to ‘lead’ the object the camera is attached to which is moving.
The issue is that the camera’s (look) direction is not always the movement direction (the player can be strafing for example).
I can’t figure out how to get the camera’s movement direction speeds… does anyone know?

I think you can use camera.getDirection() (Camera direction and position - #6 by Evgeni_Popov) and camera.speed.

Thanks for the link, I read that thread but the problem I’m having is that the player may be colliding with something and the actual movement speed of the camera is not always the speed the camera is set to…

What are you using to move the camera around? Can you share a PG with your camera movement code? If you are manually moving the camera, you can probably simply add a line to calculate the movement vector. If you are using some other approach, you may be able to track the position of the camera in the last frame, compare it to the current frame, and use that vector.

Something as simple as this might work, depending on your implementation:

 const lastPos = new BABYLON.Vector3.Zero();
    scene.onBeforeRenderObservable.add(() => {
        const vec = camera.position.subtract(lastPos);

It’s hard to say more without seeing your code.

1 Like

Thank you, I was thinking of this solution but I thought the vector might be stored somewhere already.

I was indeed manually moving the camera (universal camera).
This solution works for what I’m trying to do.

1 Like