Quaternion.Slerp turning at constant velocity

Hello, I am using this code to interpolate between two rotations:

transform.rotation = Quaternion.Slerp(transform.rotation, finalRotation, Time.deltaTime*5);

It works fine, but the problem is the rotation speed isn’t constant, it rotates fast at the start and then gradually slows down.
I need it to rotate at the same speed from start to finish.

Thank you for any advice

Do you understand what Slerp does?

Slerp(a, b, t) will give you a rotation that’s moved t from a towards b - so if t is .5, you’ll get something that’s rotated halfway from a towards b.

To get a contant rotation from a to b, you’ll have to move t from 0 to 1 over time. You, on the other hand, have a constant value for t, and are moving a.

Can you provide an example code?
If i keep rotation constant and instead change the time in for example 10 steps (0.1s, 0.2s) etc the turning speed will be exactly the same for all distances. I need it to turn at a constant speed no matter if the two rotations are only one degree apart or 180 degrees apart (think weapon turret, so if turning 10 degrees takes one second, turning 100 degrees needs to take 10 seconds)

You’ll need to find out the angle between the 2 quaternions first if you want it to slerp at a constant speed. Try this function to get the angle: Unity - Scripting API: Quaternion.Angle

The ‘t’ value you pass into Slerp should be something you keep track of yourself. The speed you need to increment it by depends on how fast you need it to be.

e.g. if you want it to move at 10 degrees a second

float DegreesPerSecond = 10.0f;
float QuaternionAngle = Quaternion.Angle( StartQuat , EndQuat );
float TSpeed = DegreesPerSecond / QuaternionAngle;
TValue += TSpeed * Time.deltaTime;

EDIT: Just noticed what ‘compressed’ said above - you’ll need to make sure you don’t feed the result of your slerp back into the function or it’ll speed up as you said.

Almost cost me my sanity but i finally figured it out! Quaternion.RotateTowards does exactly what i need

1 Like

Thank you =)