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;
}
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. – DMGregory Jul 30 '22 at 21:35