Uniform rotation - uniform lerp

Hi,

I am pretty familiar with the lerp function and its variants by know, but am now in need of creating a specific kind of rotation, with equal/ constant angular speed throughout the whole transition.

Basically, I need to rotate a transform from a to b with uniform speed, or with acceleration control, alternatively. The lerp function instead ‘accelerates’ and ‘decelerates’ when it nears the ‘to’ value, which does not fit my purpose.

Despite my numerous attempts, I was unable to come up with a solution.

What should I look for to achieve this?
Is it possible to create a constant angular speed rotation with Unity’s builtin functions, or do we need a special formula? Alternatively, what’d be the way to dynamically alter the T parameter in lerp to at least ‘simulate’ uniform speed?

I’m grateful for any advice. This problem is driving me mad.

It helps to know that Lerp and moveTowards are only a line or two of code each. They aren’t doing anything special. The angle versions only have an extra line or two checking for wrap-around (ex: 350 degs is only 20 away from 10 degs):

A=Lerp(B, C, 0.1): A = B +(C-B)*0.1 // A is 10% of the way from B to C

A=moveTowards(A, B, amt): A=A+amt; if(A>B) A=B; // add amt to A, but not more than B

Using your current value as an input a=Lerp(a, b, 0.1); allows you to call it repeatedly, moving you 10% closer each time. That’s a cute trick. It’s pretty much the same thing as saying speed=speed*0.9; to create friction, which affects you more when you are going faster. Of course, instead of Lerp, you could just write: a=a+(b-a)*0.1;

The two ways of moving at a constant speed are 1) adding a constant value to your angle, using RotateTowards, 2) Fixing start and stop, and changing the percent from 0% to 100%, using Lerp. The adding method is easier, but can get math errors (0.1+0.1… ten times is ten tiny rounding errors away from one.) You’ll get there, but maybe a frame more or less than you really should.

MoveTowards method:

  // do this anytime to start it:
  target = whatever

  // each frame:
  angle = Quaternion.RotateTowards(angle, target, speed*Time.fixedDeltaTime);

Lerp method:

// do this anytime to start it:
float pct=0;
Vector3 start=transform.rotation; // save start angle
Vector3 end = whatever

// each frame:
pct=pct + speed*Time.fixedDeltaTime;
if(pct>=1) pct=1;
else { transform.rotation = Quaternion.Slerp(start, end, pct); }