Setting an objects Z rotation to a whole number or integer?

I have a 2D object that I want to set the Z rotation of to a whole number.

In the console this returns whole numbers:

        float newZ = -angle + 1;
        newZ = Mathf.Round(newZ * 100f) / 100f;
        Debug.Log("newZ = " + newZ);

Now I try to set the objects rotation to this ‘newZ’ value:

        platform.transform.eulerAngles = new Vector3(0, 0, newZ);

When I run the game the object’s rotation immediately sets to ‘0.9710001’ when the newZ value returns ‘1’ in the console.

Can anyone explain to me why this is happening? How can I go about setting the Z rotation to a whole number?

Thanks,
Corey :slight_smile:

Floats are prone to rounding errors, and that is probably what you are experiencing here. Have you tried skipping the 100f parts? Shouldn’t a simple; newZ =Mathf.Round(newZ) be just as good and less likely to cause rounding errors. You could even try to cast it as an int.

I have removed the 100f parts because it turns out they were doing nothing (I’m a rookie lol)

I have also written the following code and it seems to be turning the float into an int.

        float newZ = -angle + 1;
        newZ = Mathf.FloorToInt(newZ);

But I am still seeing the issue :frowning: I’m going to create a blank project and see if I get the same issue.

EDIT: Blank project gives me the same issue, I am obviously missing something here, probably something quite obvious :stuck_out_tongue:

first off I need to point out that rotations aren’t stored as euler angles internally. So when you set eulerAngles or read eulerAngles, a conversion is happening. The rotation is actually stored as a Quaternion which is a 4-d vector made up of complex numbers (a + bi type complex numbers). The conversion to and from quaternion to euler isn’t perfect due to float errors as well as the trigonometry involved (trig is problematic in discrete systems like a computer). So you should expects slight inaccuracies here and there.

Thing is 0.9710001 is an inaccuracy of 0.289999, which is a bit high.

And it’s not the value I’m getting on my machine.

Which makes me wonder what -angle is. What’s its value? Can you show us the full code?

Thank you for the explanation, anything I can learn from this issue is appreciated, I’m still a rookie at this point so I apologize for any seemingly obvious issues to you.

The thing that’s strange is that I am getting the same inaccuracy on a blank project, the inaccuracy is still 0.028999.

Below is the code for ‘angle’, but it is converted to an int so it shouldn’t be causing any issues I don’t think.

        angle = transform.localEulerAngles.z;
        angle = (angle > 180) ? angle - 360 : angle;
        angle = Mathf.Round(angle);
        angle = Mathf.FloorToInt(angle);

EDIT: The blank project consisted of a 2D sprite with a script attached that just had the following code in the start function:transform.eulerAngles = new Vector3(0,0,1);

I have found another way to achieve what I wanted using Hingepoints but if anyone can answer this question it could be useful to know in the future for something maybe :slight_smile: