🤔 Unity, huh, how?

🤔

Wrong-lerp

Lerp is linear, if t changes at a constant speed, so does the output.
Wrong-lerp is an application of lerp that produces smooth, yet imperfect movement towards a target value.

value = Mathf.Lerp(value, target, Time.deltaTime * speed);

Accompanied by such phrases like:

🤓 "Your using Lerp wrong."

This common hacky application easily creates smooth motion.

Downsides

Vague is the takeaway from this usage.
If you don't need exact outcomes or durations and aren't too worried about differences across frame rates, applying lerp like this is a common creative way to smooth movement.

Graph

10fps20fps50fps100fps
0.00.10.20.30.40.50.60.70.80.91.0↑ Value00.050.10.150.20.250.30.350.40.450.5Seconds →
// speed: 10
value = Mathf.Lerp(value, 1, Time.deltaTime * speed);

Improvement

Using a more complex t can solve frame rate dependency problems.

static float ExponentialDecay(float value, float target, float decay, float deltaTime)
    => Mathf.Lerp(value, target, Mathf.Exp(-decay * deltaTime));
Where decay is 0->∞. 0 is constant, and larger values approach the target faster.

Graph

10fps20fps50fps100fps
0.00.10.20.30.40.50.60.70.80.91.0↑ Value00.050.10.150.20.250.30.350.40.450.5Seconds →
// decay: 10
value = ExponentialDecay(value, target, decay, Time.deltaTime);

See also: Fractional approach

// Using a "fraction" or "remainder" as input.
static float FractionalDamping(float value, float target, float fraction, float deltaTime)
    => Mathf.Lerp(value, target, 1 - Mathf.Pow(fraction, deltaTime));
Where fraction is a 0->1 smoothing factor.
0 gets you the target (no smoothing), 1 is the source (so smoothed it's pointless).

Using Pow is a more expensive approach, so consider your use cases.

Conclusion

If you are concerned about any of the downsides, consider alternatives like:

*Certain libraries will have their own tweening libraries built-in. For example, UI Toolkit has USS transitions.

Other resources


Return to overview.