# Lerp velocity in specific time

How to lerp velocity to 0 in specific time? For example in two seconds? I need this for jumping.

velocity.Y = MathHelper.Lerp(velocity.Y, 0, [time]);

• This looks like XY problem - are you sure you did not wanted to ask how to implement simple gravity? – wondra May 28 '16 at 17:57
• Your problem translates into traversing the (0:1) range in a given amount of time. This should be your starting point. Have you considered doing this? The comments and answers seem to point out specific details you might have not given or overlooked. – teodron May 28 '16 at 18:55
• Technically speaking, it's impossible to lerp to 0 – Steve H May 29 '16 at 13:17

float Timer = 2f;
float TIMER = 2f;
float StartVelocity = 10;
float EndVelocity = 0;

void Update()
{
float elapsed = (float)gameTime.elapsedSecondsOrWhatever;

Timer -= elapsed;
if (Timer < 0)
Timer = 0;

velocity.Y = MathHelper.Lerp(EndVelocity, StartVelocity, Timer/TIMER);
}


Your question is vague (could you please elaborate?), so I'll assume that you don't know how lerping works and that you don't have access to the math function, then you can write your own like this:

float lerp( float value1, float value2, float alpha, float range ){

alpha = alpha/range;

if( alpha < 0 )
alpha = 0;
if( alpha > 1 )
alpha = 1;

return value1 + (alpha) * (value2-value1);
}


Optionally, using alpha blending:

return (1-alpha) * value1 + alpha * value2;


Then, you can call

velocity.y = lerp( velocity.y, 0, delta_time, 2.0);