Camera movement independent of rotation

Hi, I have a camera which moves left right up and down using mouse getAxis.

I get the the mouse axis values and add them to a variable called movementOffset. The camera moves fine when it has no rotation, but when i rotate it, the camera moves in weird directions rather than left right up and down. How can I translate the movementOffset in such a way that the camera always moves left right up and down no matter what the rotation is.

How I move:

movementOffset += new Vector3(Input.GetAxis("Mouse X"), 0, Input.GetAxis("Mouse Y"));
transform.position += movementOffset;

In short, No matter what the rotation of the camera is, I always want it to move up down left and right according the mouse coordinates…

If your camera’s primary motion is always orbiting around the +Y axis, all you need between these lines is to multiply the input motion by the camera’s rotation before you add it to the position.

Insert these lines between the above two lines and see how it goes:

// this is which way around the Y we're facing
float facing = Camera.main.transform.rotation.eulerAngles.y;

// rotate the motion to the camera's orientation
movementOffset = Quaternion.Euler( 0, facing, 0) * movementOffset;
1 Like

Thank you, that worked perfect :smile:

1 Like