0

I have fragment of code that has to rotate the camera to the direction where my cursor is as below:

float yaw = mouseSensitivity.x * Input.GetAxis("Mouse X");
float pitch = mouseSensitivity.y * -Input.GetAxis("Mouse Y");

Vector3 camRotDir = new Vector3(pitch, yaw, 0);

// this works 
Vector3 camRot = mPlayerCamera.transform.rotation.eulerAngles + camRotDir;
mPlayerCamera.transform.rotation = Quaternion.Euler(camRot);

// this doesn't work
//var rotation = Quaternion.Euler(camRotDir);
//mPlayerCamera.transform.rotation =  mPlayerCamera.transform.rotation * rotation;

WithmouseSensitivity being an arbitrary Vector2 value representing mouse's sensitivity respectively on axis X and Y.

The problem is when I convert the current camera's rotation in Quaternion to Euler angle, add the camRotDir to it and then convert the result back to Quaternion, it works properly, the z component in camera's rotation remains unchanged during this process. But when I first convert the camRotDir to Quaternion and add it to camera's current rotation by multiplying them together, I get unexpected behaviour, the z component was somehow changed.

I have no idea why this happens, should Quaterion of (euler_a + euler_b) == (Quaternion of euler_a) * (Quaternion of euler_b) ?

Code run in Unity 2018.3.12f1.

tc07
  • 455
  • 2
  • 9

0 Answers0