Slerping rotation mirrors
- by Esa
I rotate my game character to watch at the target using the following code:
transform.rotation = Quaternion.Slerp(startQuaternion, lookQuaternion, turningNormalizer*turningSpeed/10f)
startQuaternion is the character's current rotation when a new target is given.
lookQuaternion is the direction the character should look at and it's set like this:
destinationVector = currentWaypoint.transform.position - transform.position;
lookQuaternion = Quaternion.LookRotation(destinationVector, Vector3.up);
turningNormalizer is just Time.deltaTime incremented and turningSpeed is a static value given in the editor.
The problem is that while the character turns as it should most of the time, it has problems when it has to do close to 180 degrees. Then it at times jitters and mirrors the rotation:
In this poorly drawn image the character(on the right) starts to turn towards the circle on the left. Instead of just turning either through left or right it starts this "mirror dance":
It starts to rotate towards the new facing
Then it suddenly snaps to the same angle but on other side and keeps rotating
It does this "mirroring" so long until it looks at the target. Is this a thing with quaternions, slerping/lerping or something else?