- Home /
How to calculate transform.localEulerAngles.x as negative value?
I have an object at point 0,0 and when I moved my mouse to the left side of the screen to rotate the object, transform.localEulerAngles.x is display values such as 359, 358, and etc. I was hoping that it displays -1, -2, and so on.
Language: C#
All your angles will exist from 0 and 359 because this covers every degree in the rotation (circle). There are technically no negative values to be had. There is some nice background magic going on in the Unity editor with the transform display that allows the display of values like -90 in the inspector when really this is equivalent to 270.
Reading localEulerAngles can be tricky since a single physical rotation has multiple euler angle representations, and Unity may change the representation on you. First if you are doing your own rotation, then you can maintain your own Vector3 and never read localEulerAngles. If you maintain your own Vector3 in any format you want including axes that go from -180 to 180. Second, you can normalize any euler angle into -180 to 180.
Answer by Tomer-Barkan · Oct 13, 2013 at 05:51 AM
If you want to get the negative values for the left side, simply do the following calculations:
float angle = transform.localEulerAngles.x;
angle = (angle > 180) ? angle - 360 : angle;
Wow. I cannot believe how easy that was. Thank you for providing the solution.
sure. Just note you'll jump from -179 to 180 at some point.
Nice solution $$anonymous$$er Barkan, it saved me from getting late to work!
$$anonymous$$ore useful than anyone would ever know!
Your answer
