Quaternion.RotateTowards stopping precision.
I am currently updating the rotation of my camera in Update
like this:
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime * RotationSpeed);
However, the transform.rotation
never becomes targetRotation
for some angles.
targetRotation
is constant and the rotation looks correct.
I understand, that Quaternion
s use floating point numbers, so some imprecision is expected, so I have tried comparing the values with
Mathf.Approximately(0, Quaternion.Angle(transform.rotation, targetRotation))
and
Quaternion.Angle(transform.rotation, targetRotation) <= Quaternion.kEpsilon
and
Quaternion.Angle(transform.rotation, targetRotation) <= Mathf.Epsilon
But the RotateTowards
function sometimes stops, with an angle of up to 0.2
degrees between transform.rotation
and targetRotation
, which is much bigger than even Quaternion.kEpsilon
, which is 10E-6
on my machine.
So how am I supposed to check, when the rotation is finished? Of course, I could just choose an arbitrary epsilon
value and hope, that it is big enough, but this seems wrong.
I think the problem is with the assignment itself. ( transform.rotation = ...
)
But I can't quite figure out, what is wrong with it.
Update:
Minimal non- working example
private void Update()
{
Quaternion target = Quaternion.Euler(0f, 90.0001f, 0f);
transform.rotation = target;
Debug.Log("Angle = " + Quaternion.Angle(transform.rotation, target));
}
Prints Angle = 0.03956468