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

Hello , 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 tried 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.

Ok i got the answer to this problem here :

rotation - I'm rotating an object on two axes, so why does it keep twisting around the third axis? - Game Development Stack Exchange

infact this was a response to this same question that i posted on gamedev.stackexchange :