Rotation around X axis cause Z axis to slightly change too

Hello everyone !

I am encountering an issue and I can’t get my head around it as it seems so simple and stupid.

I am rotating an object in the world ( a pivot ) using as an input the mouse X and Y axis.

it looks basicly like this :

float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
float vertical = Input.GetAxis("Mouse Y") * rotateSpeed;

pivot.Rotate(0f, horizontal, 0f, Space.World);
pivot.Rotate(vertical, 0f, 0f, Space.Self);

this is causing the pivot rotation on Z to have really small values like ‘0.000025’ changing as I am moving the mouse.

Does anyone know why and what is causing this value to change ?

PS : I tried without specifying the space reference, the value is still changing

Thank you !

Because Rotations. Are. Weeeeeird!
I don’t like using transform.Rotate, just because it’s easy for small errors to creep in over time. I’d much rather store the total cumulative “distance” the mouse has moved in each axis, and start the rotation anew each frame, something like

pivot.rotation = Quaternion.Euler(vertical, horizontal, 0f);

Hello !

I tried with pivot.rotation and there is still values on the Z axis appearing ( in addition to making rotate the pivot with the mouse and then automatically weirdly trying to get back to its original rotation :stuck_out_tongue: I think this is due to the fact that the inputs change from 0 to 1 and then back to 0 when not moving the mouse )

Maybe I could try to lock to 0 the Z axis rotation and force it on every frame, but it looks such a hacky way to do this…

If the rotation looks like it’s moving correctly, and the errors aren’t cumulative, then do you care? Try spinning the thing around a bunch - does it still look right after a bunch of rotations?

The reason small values are most likely appearing has to do with the conversion from Quaternion to Euler angles for display in the inspector. Converting to Euler angles is notoriously problematic (fundamentally so, not just in Unity) so it wouldn’t surprise me if odd numbers start appearing in the inspector. Ignore the inspector for rotation, and focus on the actual in-game result.

1 Like

I see, problem is these values are getting up to -0.1 sometimes, and I am using a system where I switch camera view often, but each time it is adding an offset to the camera causing some big issues controlling the camera.

I am not sure if thats specifically this rotation on Z value causing this, but I figured I wanted to understand first why this values kept changing for no reason.

I checked in the inspector and no values are displayed. I am getting these values by using

Debug.Log(pivot.transform.rotation.z);

rotation.z is something completely different. Rotations are quaternions, with w/x/y/z values between -1 and 1. These w/x/y/z values bear no direct relation to the x/y/z values you would expect to see, which are known as “Euler angles”. (I mean, as a whole, the WXYZ represents the same rotation as Euler’s XYZ, but X isn’t X, Y isn’t Y, and Z isn’t Z) Check the article I linked last comment for a more detailed explanation.

Short story is, there’s no problem here unless you’re actually observing unexpected rotational behavior.

Oh wow, this is eye openning to me, I should open the debug inspector more often :confused: Thanks a lot

So following the logic where we naturally never want to use euler angles except if we are not manually entering angles ourselves, using Quaternion.Euler is still somehow broken since we are inputing euler angles, is that correct ? We should instead use the Quaternion.AngleAxis to have no problem with priorities and order in which objects will be rotated

PS : After I checked there is indeed no issues what so ever with the Z rotation… the issue is coming from the fact that I am using an interpolation ( Quaternion.Slerp ), and the interpolation obviously making mistakes with the axis to blend, even though there is just a rotation on the Y axis