# Smooth/progressive transition between 2 floats in a certain amount of time

I’m trying to get an initial speed (let’s say, 20 units) to a target speed (say, 10) smoothly in 0.2seconds.

I used lerp methods like this one Mathf.Lerp(initial velocity, target velocity, time), but they don’t seem to work as I expected.

Is there anyone who knows well how to implement the Mathf.Lerp function (or maybe other methods to achieve this)?

So far, I was suggested the following code, that I have in an Update loop :

private void Update()
{
if(!_stop) return;

velocity.x = (Mathf.Lerp(initialVelocity, targetVelocity,
time) * Time.deltaTime;
time += 0.5f * Time.deltaTime;

if(time > 1)
{
time = 0f;
_stop = false;
}

}

and I tried changing the time values, but I never got my expected lerp in .2s since I don’t know precisely how this multiplier is working, or what the time in the lerp function represents exactly. I hope anyone can let me know. Thanks

1 Like

As an aside question, the blogger in this article said that when using lerp, sometimes the movement gets stuttery and jerky in the editor, and indeed it happened to me as well:

As he said, it works great (smoothly) in the build. But I wonder if you (or anyone) know of a fix to this, because mostly all the testing is done in the editor, and it doesn’t feel great to deal with this stuttery movement all the time. Thanks

Smoothing movement between any two particular values:

You have currentQuantity and desiredQuantity.

• only set desiredQuantity
• the code always moves currentQuantity towards desiredQuantity
• read currentQuantity for the smoothed value

Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

The code: SmoothMovement.cs · GitHub

Another approach would be to use a tweening package line LeanTween, DOTween or iTween.

For score count-up (like a cash register whizzing lots of numbers upwards), I like this approach:

Fading, simple and easy:

1 Like

@Kurt-Dekker Quick question. I want to try your implementation, but it says that only the desiredQuantity has to be set. What if I want this smooth transition to happen in 0.2 seconds, which other value should I modify?

EDIT: Maybe I should set this const float MovementPerSecond = 2.0f; value to 0.2f?

If you are at A and you want to get to B, you must change by the amount (B - A).

If you want to change that in T seconds, then the movement rate would be:

MovementPerSecond = (B - A) / T;

You would compute that ONCE, at the point where you begin the movement, obviously.

What’s the point of the Time.deltaTime multiplication at the end? It makes no sense, remove it

ps: your code would not compile as you have two opening brackets in that line of code and just one closing. So this does not add up.

pps: this line of code is correct, but will make the transtion last for 2 seconds:

time += 0.5f * Time.deltaTime;

The overall logic would be

time += Time.deltaTime / duration;

So if you want the transition to last 0.2f seconds, just set duration to 0.2f. Though dividing by 0.2 is the same as multiplying by 5. So you could also do

time += 5f * Time.deltaTime;

to get the same result ((1f/0.2f) == 5f)

2 Likes

That makes sense, all clear then, thanks. I’ll let you know how it works here.

Tying what Bunny wrote above into what I said about (B-A)/T:

• with Lerp() alpha, you are changing the alpha by a quantity of 1.0 for full deflection
• Bunny’s code is moving by 0.5f per second
• A = 0
• B = 1
• T = 2
• so 1.0f / 0.5f —> 2 seconds, as Bunny correctly states

Or put another way, 1.0f / 2 gives you a rate of movement of 0.5f

Math!

1 Like

It’s needed in this case because I’m computing the velocity right there; then in my Move() method I just do transform.Translate(velocity); (so the Time.deltaTime is calculated earlier)

Probably I made a typo while writing the code here from scratch, sorry about that!

Thank you for letting me know how that “time” from the Unity’s example works! that was very instructive, I’ll give it a try.
PS: do you know why this provokes the stuttery movement in the Editor?