Lerping With Delta Time

What is the standard accepted way to incorporate deltaTime with a mathf lerp like this:

    public float testNumber = 0;
    void Update()
    {
        testNumber = Mathf.Lerp(testNumber, 150, .1f);
    }

In your case you need to multiply .1f with Time.deltaTime.

1 Like

AHHHH, I knew it was something simple like that, thank you!!!

This depends on your desired “curve”.

If you want to have the curve “spring” toward the target value - starting moving fast immediately and then ease towards it at the end, then what you have will more or less work.

However, it won’t be consistent with varying framerates; a slower machine will lerp towards the target more slowly. Multiplying it with Time.deltaTime doesn’t quite do the trick either in this case. It actually has the opposite effect; a high framerate will approach the target more slowly than a low framerate.

To make it framerate independent, you have to do a little math:

testNumber = Mathf.Lerp(testNumber, 150f, 1f - (1f / (lerpSpeed * Time.deltaTime) + 1f));

That formula will get you a consistent % of the way towards the target in a given time period, regardless of the current framerate. lerpSpeed is a constant, and probably should be around 5ish or higher (which will get you a little over 90% in one second).

If your goal is a constant movement, you can either:

  1. Get the start and end positions and use those as your first parameters in Lerp, with an ever-increasing value for the third parameter. (This is how Lerp is intended to be used)
  2. Use Mathf.MoveTowards instead of Lerp with a constant speed * Time.deltaTime in the third parameter.
1 Like