Rotation problem: Check is failing/ endlessly spins

Hello Unity forums,

I have a strange logic error i’m not seeing.

I want to rotate a cube with the push of a button, and when I release this button I then want the cube to automatically rotate backwards to a 90 degree side (0,90,180,270,360).

What I have now works occasionally, however more often then not it just spins endlessly! What’s more I was tracking the rotation, and it actually is fractional when incrementing below 90 DESPITE the fact that I am rotating on an integer value of “1”

ex:
80.000009
81.000009
82.000009

Below is an example of my check:

if (rotObject.transform.eulerAngles.y % 90 != 0 && !rotateYActive) {
Debug.log(compensating for rotation " + rotObject.transform.eulerAngles.y);
rotObject.transform.Rotate(0,-rotationSpeedY, 0);
}

Thoughts? Funny math? data type problem?

Hi,

Could you not work this out using maths alone? for example:

  1. rotate to a Y value (Eg: 286)

  2. divide that number by 90 (3.17777777777)

  3. store that as an int (3)

  4. multiply by 90 (270)

Off the top of my head I can’t think of any problem areas and you could then either rotate directly or lerp to that rotation value and it will always be the closest 90

This method would allow you to always have the exact multiple of 90

I hope that helps?