Help with Quaternion.AngleAxis

So I’m trying to get my Camera to rotate around a single game object when I hold down the Middle Mouse Button.

My script works fine when rotating the camera around Vector3.up, but when rotating around Vector3.forward the camera moves up and down even though I am only moving the mouse down.

The problem I can see clearly, as the camera rotates, its “forward” rotation axis changes (this doesn’t happen when rotating around “up” axis).

The solution? No idea, thanks in advance for your help! :slight_smile:

Here’s the code:

private void Update()
    {
        if(Input.GetMouseButton(2))
        {
            camTurnAngle = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * rotateSpeed, Vector3.up);
            camTurnAngle = Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * rotateSpeed, Vector3.forward);
            camera_offset = camTurnAngle * camera_offset;
        }
        transform.position = targetShip.transform.position - camera_offset;
        transform.LookAt(targetShip.transform);
    }

If you’d check your code you can clearly see you overwrite the camTurnAngle value with the second Quaternion.AngleAxis.
You should multiply quaternions if you want them to ‘stack’. But bear in mind that quat multiplication is not commutative eg q1 x q2 does not equal q2 x q1