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]);
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.
Sign up to join this communityHow 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]);
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);