# Transform.Rotate() rotation about one axis also rotates about the other axes [duplicate]

i'm new to learning unity and infact game development.Below i have setup a scene to learn about unity and i have encountered a strange problem that doesn't comply with anything that unity has documented.

I am rotating the camera (which is a child of my capsule game object, The object with Gradients highlighted in the photo, Capsule game object is a child of nothing) according to the user mouse movements.Here is the code i have written(Please ignore the commented lines for now)

      Vector3 a;
rotateby = Input.GetAxis("Mouse X") * MouseSensitivity;// Rotate by is a float variable
rotation.Set(0, rotateby, 0); // Rotation is a Vector3 i've declared
transform.Rotate(rotation, Space.Self);

/*       a = transform.localRotation.eulerAngles;
a.Set(0, transform.eulerAngles.y, transform.eulerAngles.z);
transform.eulerAngles = a;
*/

rotateby = Input.GetAxis("Mouse Y");
if (!invertY) { rotateby = -rotateby; }
rotation.Set(0, 0, rotateby);
transform.Rotate(rotation, Space.Self);

/*       a = transform.localRotation.eulerAngles;
a.Set(0, transform.eulerAngles.y, transform.eulerAngles.z);
transform.eulerAngles = a;
*/


As you can see from the code when the user moves the mouse across Horizontal axes , i'm only updating the angle of rotation across the capsule's y-axis( rotation.Set(0, rotateby, 0) ) without updating the other two angles(x and y), which rotates it across its y axis.Similarly when the user does vertical mouse movement i'm updating only the z-angle of the capsule( rotation.Set(0, 0, rotateby) ) without updating the other two angles(x and y) again, so now it rotates across it's z-axis.Now it does what is expected, it is infact rotating across its z and y axes but it is also rotating across the x-axis in some cases.I am monitoring the values of the three angles(x y z) from the inspector.Look at how the capsule is titled towards the right , across its x-axis and also from the inspector below(look at the x-angle = -35)

Now the commented lines of code are a solution to this problem of rotating around the x-axis(as when the x-angle changes i'm again setting it to 0).But what i need to know is what have i done wrong in my code i'm never modifying the x-angle then why is it changing to -35??.I have read about local vs GlobalSpaces again and again but i can't find the answer to this.Also what i think is that it's not a Gimbal Lock because as the parenting order that unity uses is Z-->X-->Y.So Gimbal lock could only occur if i rotate around the X-axis(Which i am not doing in my code) thereby aligning the Y-axis with the Z-axis.Why is this behaviour occuring, why is it rotating around its x-axis .Please Correct me if i am wrong, this is a barrier to my learning process cause i can't continue without clearing my concepts.

Thankyou, much obliged !

UPDATE: I have trieed changing the 2nd parameter in transform.Rotate() method to Space.World , the problem of rotation about x-axis still happens but in some different mouse movement order.

• We have several other questions & answers on this topic I'd recommend you read. The short versions: rotation in 3D is not commutative. If you stack up a bunch of pitches & yaws over many frames, the result will diverge from applying all the pitch then all the yaw: you'll get some roll too, because the axes you're rotating around are rotating with your object when you use Space.Self. Common fixes are to "pitch local, yaw global," accumulate pitch & yaw separately and compute a rotation using their total, or to apply correcting anti-roll rotations gradually if using 3D flight controls. – DMGregory Jan 20 '17 at 15:00
• @DMGregory i'm sorry i still can't understand the issue of roll , although the axes are rotating with the object, but as i'm rotating the object with respect to it's own axes not the Global ones so it should still pitch and yaw , i can't visualize that any rotations around z or the y axis could make the object roll – With A SpiRIT Jan 20 '17 at 17:27
• I can understand why this should be a problem when you're rotating the object relative to the Global coordinates, but if you move the object with respect to its own axes then rotation around its y-axis will always yaw and rotation around its z-axis will always pitch(No way to roll).Regardless of whether the local axes rotate with the object the object will still see it's z-axis as z , y-axis as y and x-axis as x and thus should behave consistently.In the unity scene view i am experimenting with this , it's easy to see why global rotation could face such a problem , but not local rotations. – With A SpiRIT Jan 21 '17 at 4:27
• Also what i have noticed is that when you rotate the object around its x or z axis by holding the orbits(rings) around the object all three angles change by a certain amount in the inspector.If you hold on to the ring corresponding to the rotation around y-axis then only the y-angle changes in the inspector view.I think this has got something to do with the rotation order and parenting structure in unity i-e(z-->x-->y), OR is this some kind of bug? – With A SpiRIT Jan 21 '17 at 4:29
• I have read the answer you hvae posted in the link you have left above in your comment.I visualized what you were conveying and indeed pitch (x rotation) 90 -->> yaw (y rotation) 90 == yaw 90-->> rolling (z rotation) 90 . Both these rotational orders end up in the same orientation of the object.But when you said "composing local x & y rotations, compounded from frame to frame, can turn into z rotations.". I could'nt get you, even if it turns into z rotations it should still leave the object in the same orientation as what we have seen above from your experiment. – With A SpiRIT Jan 21 '17 at 9:12