Slerping rotation mirrors

Posted by Esa on Game Development See other posts from Game Development or by Esa
Published on 2013-10-23T05:42:06Z Indexed on 2013/10/23 10:18 UTC
Read the original article Hit count: 284

Filed under:
|
|
|
|

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:

enter image description here

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":

  1. It starts to rotate towards the new facing
  2. 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?

© Game Development or respective owner

Related posts about c#

Related posts about unity