Mathf.MoveTowardsAngle is not working as predicted

I’m using ‘Mathf.MoveTowardsAngle()’ to rotate an object towards an angle(zero). Sometimes it works as expected and other times it sets the rotation to a value close to zero(but not zero). I’m assuming this is because I’m using eulerAngles but input on why this is happening would be much appreciated. Here’s a snippet of my code:

 /*targetRotation = 0*/
             float angle = Mathf.MoveTowardsAngle(myTransform.eulerAngles.z, targetRotation, moveSpeed * Time.deltaTime);
             myTransform.eulerAngles = new Vector3(myTransform.eulerAngles.x, myTransform.eulerAngles.y, angle);
 
             if (targetRotation == myTransform.eulerAngles.z)
             {
                 //Do Something(In my code, it just changes a enum value)
             }

Also I should note that this formula is placed in update and the rgidbody2D.fixedAngle of the object is set to true before running this(I set it true in order to negate the effects of physics on its rotation when it’s moving towards the angle).

That is expected, the translation from Euler angles to Quaternion and back is not lossless - rounding errors cause slight variation of the results, and also the Euler angles are normalized into a specific set of ranges which can lead to sharp changes in value (e.g. 360 => 0).

You can either add some leeway to your “if”, or test like this instead:

if (targetRotation == angle)
{
    ...
}

which should work for your case. It would not necessarily work, though, for Y axis rotations, nor for certain key angles about any axis, due to the sharp jumps in Euler angle values at those points.