Is there a "Reach this value but at a maximum speed of"?

Correct me if I’m wrong, but with Lerp and SmoothDamp you can tell the game to try and change one value (e.g. float) from one value to another (10 → 50) over a set amount of time, right?

Is there a way to change a value (again, float, 10 → 50) but at a maximum speed of X, independent on how long that will take?

Example.

Go from position.x = 10 to position.x = 50 with a maximum speed of (variable) 2, which would then take 20 “units” (I guess frames).

The best way for it would be (I think) if I could input: Start value, Target value, speed and deltatime

Thanks,

From the docs, one of the overloads:

static float SmoothDamp(float current, float target, float currentVelocity, float smoothTime, float maxSpeed, float deltaTime);

maxSpeed: Optionally allows you to clamp the maximum speed.

To get mathsy:

Speed is a quantity divided by time. In terms of travel for instance, its distance/time.

So since lerp requires time, rearrange:

speed = distance/time

time = distance/speed

In your case, the “distance” is the difference between your start value, and your end value, so it becomes:

(end_value - start_value)/speed

or for your example:

(50 - 10)/speed

(Bearing in mind this is per second)
So if you set your speed to 2, it would lerp in 20 seconds, which makes sense.

And then if you want that faster than seconds, you’d just divide the whole lot again by some number (i.e. for every frame, you’d divide by deltatime).