// Update is called once per frame
void Update () {
if (Input.GetKeyDown (“q”))
{
transform.Rotate (transform.rotation.x,transform.rotation.y,transform.rotation.z-90);
}
}
}
Even though I’m telling it to rotate exactly 90 degrees, the object actually rotates by 90.7071 degrees. By the 3rd rotate, the object is visibly messed up due to this.
How do I fix this to ensure that it is rotating exactly 90.0000 degrees?
Code tags. Absolutely necessary, but because this is so short, I’ll answer: floats have a certain “floatiness” to them that does not lend them to this kind of precise calculation ( or checking exact equality like “if(someFloat == 0f){}” ). If you want to rotate in exact increments, you need to do some rounding (Mathf.RoundToInt()) or parse the current rotation into an “int” and assign it back again.
I would check that the initial rotation is properly zeroed out. Also, because this question is now slightly annoying me in its refusal to be solved, I made the following:
public Vector3 RoundToNearest90Degrees(Vector3 oldAngle)
{
int newX = Mathf.RoundToInt(oldAngle.x / 90) * 90;
int newY = Mathf.RoundToInt(oldAngle.y / 90) * 90;
int newZ = Mathf.RoundToInt(oldAngle.z / 90) * 90;
return new Vector3(newX, newY, newZ);
}
This is for the eulerAngles, not the quaternion rotation.