- Home /
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).
Answer by gfoot · Feb 25, 2015 at 11:08 PM
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.
I tried changing the if statement as you suggested but it's displaying the same results as my current if statement; the enum didn't change. Is there a way to evaluate the Quaternion directly so that there are no variation in the results?
Your answer is correct, it is not possible for me to achieve the level of precision I want using Euler angles. So I decided to use Quaternion.Lerp and I've been able to achieve the results I wanted so far. Thank you for your Input.
Great! Two other things you could consider - if nothing else changes the orientation, you could store the Z rotation in a member variable and update it from there. It won't work so well if other things drive the orientation of your gameobject though.
The other is to consider using Quaternion.RotateTowards ins$$anonymous$$d of Lerp. It behaves a lot like $$anonymous$$oveTowards, and you can use Quaternion.Angle afterwards to see how much further there is to go.