Search code examples
c#unity-game-enginequaternions

Strange behaviour using Slerp


I am using Quaternion.Slerp to rotate a car when turning. I've noticed that right before a left turn, the car will rotate slightly right before rotating left to make the turn. I am also Lerping the car from one position to the next. Any idea why this happens and how to stop it?

car.transform.position = Vector3.Lerp(car.transform.position, nextPosition, fracJourney);

if (direction != Vector3.zero)
{
    Quaternion lookRotation = Quaternion.LookRotation(direction);
    car.transform.rotation = Quaternion.Slerp(car.transform.rotation, lookRotation, Time.deltaTime * rotationSpeed);
}

Solution

  • Quaternion.Slerp uses interpolation and shortest path. It does not work well with large angles. And is literally undefined at exactly 180 degrees. Around that angle you will get something "jerky" from frame to frame.

    Two options:

    Tween your animation. That is, effectively, rotating by smaller angles up until you reach the final angle. Think of key frames or breaking one rotation into smaller chunks.

    Or you can do the interpolation while still in Euler Angles space. And then use Quaternion.Euler after interpolation.