Have you ever noticed that there is a problem in the Unity Editor with the accuracy of the rotation of the objects? This happens to me many times when I duplicate objects. For example if an object has a rotation of 90 degrees in the y-axis, as soon as I duplicate that object, the rotation of the new object becomes 89.999.
This is not a problem as such, but it gets annoying. Has anyone found a fix for this?
There is no “fix”, it’s just how floating numbers work. They are not precise by their definition.
In depth: You can actually have exactly 90 as floating number, like many other integer values. However under the hood unity doesn’t use Euler angles for rotations, it uses quaternions. So, when you set rotation to 90 degrees it gets converted to quaternion. Quaternion consists of four small floating numbers (all of them are < 1), and these are not precise. So when editor converts them back into Euler angles to display to you small errors are inevitable.
@MSpiteri, That’s normal behaviour and not only in unity, the same in blender, for example. But if you need to compare values and you don’t want random working you may use:
private float precise = 90;
if(Mathf.Round(transform.rotation.z) == precise)
// do you stuff
By the way, it looks like this rounding error no longer manifests itself when copy-pasting GameObjects, at least here, using Unity 5.5.1f1.