I have an object which can be rotated at 0, 90, 180, 270 degrees. I am using this to get the rotation data:


However that code keeps ignoring 180 degrees and instead shows 0 or some weird calculation which is basically zero (smth like -93+2323232-4)

I have read about quaternions, eulers and unity rotations in general and every time I curse my life when I have to work with unity rotations...

Is there any human readable and efficient/easy way to get the exact rotation the object has ? Or at least to get these values from 0 to 360.


1 Answer 1


Remember, Euler angles are a fiction, made for ease of data input in the scene editor. They're not stored under the hood, so when you read an Euler angle triplet, it's converted on the fly from the actual stored quaternion representation.

The quaternion will always give you consistent behaviour to calculate with - that's why we use them. The conversion from an orientation to Euler angles is arbitrary - many different Euler angle triplets correspond to the same orientation, and the engine has to pick wrap-around points somewhere, quite likely not where you want them.

So it's best to never use .eulerAngles as the input to a calculation. Treat this field as write-only. Never read with an aim to modify the value or make decisions based on the value, or you are extremely liable to make a mistake.

If you have an object that you only ever rotate around the x axis, you can compute its rotation about that axis by checking which direction its .forward vector is pointing:

float GetXDegrees(this Transform t) {
    // Get the angle about the world x axis in range -pi to +pi,
    // with 0 corresponding to a 180 degree rotation.
    var radians = Mathf.Atan2(t.forward.y, -t.forward.z);

    // Map to range from 0 to 360 degrees,
    // with 0 corresponding to no rotation.
    return 180 + radians * Mathf.Rad2Deg;
  • \$\begingroup\$ Thanks for the help. It is probably going to be better if unity removed the euler angles read ability, if they are just giving you inconsistent data... I dont 100% understand that code, but it does shows consistent numbers. Though for some reason it shows 270 degrees instead of 90 and vice versa. Do you know why that is happening ? \$\endgroup\$ Commented Jul 30, 2022 at 19:40
  • \$\begingroup\$ Oops, sign flip on my part. Fixed. The data is not inconsistent per se. It's following a consistent rule, it's just not the one you expected, because you're using the wrong mental model. Any choice they make for where to put the wrap arounds will disagree with someone's mental model for some use case, so the solution is to update your thinking. \$\endgroup\$
    – DMGregory
    Commented Jul 30, 2022 at 19:55
  • \$\begingroup\$ The read access is still useful to print/log out for human review, or serialize into a human-readable asset file - it's easier for most folks to interpret an Euler triplet than a quaternion. You just have to be cautious about projecting your assumptions onto it. "This object says it's rotated 89.01 degrees around the x axis" is a reasonable use. Assuming "if I rotate it 1 more degree, the x Euler angle will read 90.01" is not so sound - it could have good reason to redistribute the rotation among the other axes when you cross such a threshold. \$\endgroup\$
    – DMGregory
    Commented Jul 30, 2022 at 20:07
  • \$\begingroup\$ I see... I still wish they made it more human friendly just like the values in the inspector are. I understand lots of stuff are happening behind the curtains and the subject of rotation and math is complex. But it would have been so much easier to simply have a Get_Rotation_X and Set_Rotation_X functions for example. They did spend the time to code all that magic in the inspector so... Anyway you are right, changing the way I look at unity rotations is A way to do it... just that I dont like it :D The current rotation system seems too overly complicated to me \$\endgroup\$ Commented Jul 30, 2022 at 21:08
  • \$\begingroup\$ If you want a Get_Rotation_X, you can add it as an extension method in three lines of code, as shown above, so it doesn't really need to be part of the standard API. Any users who want this can implement their own version that agrees with their own mental models, rather than standardizing one version that will necessarily chafe with somebody else's way of thinking. The inspector isn't doing anything magic, it's just holding a redundant copy of the Euler angles so it doesn't have to convert from quaternion between each edit - you can do the same in your own code if you want to pay that cost. \$\endgroup\$
    – DMGregory
    Commented Jul 30, 2022 at 21:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .