Preface: I am terrible at using quaternions right.
I wanted to create a world rotation independent mouselook script. That is, no matter the 'down' for the camera, the mouselook should feel natural to the player in relation to what is displayed on screen.
I now have the following script running on update in my camera:
Quaternion worldRot = Quaternion.LookRotation(Vector3.forward, Vector3.left);
xRot += -rotSpeed * Input.GetAxis("Mouse Y");
yRot += rotSpeed * Input.GetAxis("Mouse X");
transform.rotation =
Quaternion.AngleAxis(yRot, worldRot * Vector3.up) *
Quaternion.AngleAxis(xRot, worldRot * Vector3.right) *
worldRot;
Unsurprisingly, it tumbles all over the place. I think the issue is in basing the rotation angles on the worldRot (World rotation as seen by the camera), while then also rotating the whole by worldRot. I cannot get my head around how else to go about it, though...
My initial alternative was to use
transform.rotation =
Quaternion.AngleAxis(yRot, Vector3.up) *
Quaternion.AngleAxis(xRot, Vector3.right) *
worldRot;
But this anchors the mouse's movements to the world rather than the camera, which as described above isn't desired either.
Based on this (or, perhaps, in a completely different way), how do I make a mouselook system with an arbitrary down vector?