transform.localRotation.eulerAngles.y returns wrong value

Hello!
I have 5 objects and I need to check their Y rotation.

For the first object from the hierarchy the Y rotation are returned correctly but for the rest of the objects the rotation are completely wrong.

Code

Debug.Log(“last rotation: " + transform.localRotation.eulerAngles.x + " Y:” + transform.localRotation.eulerAngles.y + "Z: " + transform.localRotation.eulerAngles.z);

First object:
8885049--1214397--upload_2023-3-18_11-2-55.png
8885049--1214400--upload_2023-3-18_11-3-3.png

Second object:
8885049--1214403--upload_2023-3-18_11-3-13.png
8885049--1214406--upload_2023-3-18_11-3-24.png

What is wrong?
The script is attached to each ‘checkpointstart’ object but the rotation are shown correctly just for the first object.

The inspector is just a human friendly display of rotation values. It is not representative of the underlying values, particularly as rotations are handled with quaternions under the hood, and not euler angles.

You might want to just check if the axis is approximately divisible by your desired rotation (using the modulo operator).

1 Like

Everything worked fine until today when I opened the project again. :slight_smile:

Okay and? That doesn’t change the validity of my previous post.

3 Likes

How do you know that line is from the second child object? You have 4 of them there and you said there’s a script on each of them.

I would recommend, in the very least, outputting the name of the GameObject alongside it or use the 2nd argument of the Debug.Log and pass the GameObject; that way you can double-click on the console line and it’ll highlight which GameObject it’s referring to.

1 Like

It doesn’t matter… It doesn’t display correctly…

Take a look now :
8885274--1214499--upload_2023-3-18_14-37-26.png
The value for checkpointstart(3) is -3.3 in the hierarchy | In the console: 356.7
The value for checkpointstart(1) is -216.37 in the hierarchy | In the cosole: 143.63

8885274--1214502--upload_2023-3-18_14-38-15.png

8885274--1214505--upload_2023-3-18_14-38-25.png

8885274--1214508--upload_2023-3-18_14-38-32.png

Read my first post again. The inspector does not always represent what is happening under the hood. Do not rely on it.

2 Likes

-3.3 and 356.7 are the same rotation. You need to read what @spiney199 put above when he said that those values shown in the inspector are not how they are stored i.e. Euler angles are not how they are stored they are for visualisation only, everything is a Quaternion for good reason.

AFAIK these Euler angles are calculated using a rotation order of Z, X and then Y.

https://docs.unity3d.com/ScriptReference/Quaternion-eulerAngles.html
https://docs.unity3d.com/ScriptReference/Transform-localEulerAngles.html

https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/Inspector/TransformInspector.cs
https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/Inspector/TransformRotationGUI.cs

1 Like

All about Euler angles and rotations, by StarManta:

https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html