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?
EDIT1: Apparently the issue does not arise from the rotating itself. The more likely issue is that the character moves toward the facing while it rotates. As limiting the angle, between facing and target, when the character is allowed to move reduces and at times eliminates the jittering/mirroring rotation.
This of course raises more questions, why cannot the character move and rotate at the same time without issues? Code used for the movement:
transform.Translate(Vector3.forward * runningSpeed/10f * Time.deltaTime);