🤔

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 →
value = Mathf.Lerp(value, 1, Time.deltaTime * 10);

Improvement#

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

value = Mathf.Lerp(value, target, 1 - Mathf.Pow(fraction, Time.deltaTime));

Where fraction is a 0->1 factor that defines a percentage of smoothing. 0 gets you the target (no smoothing), 1 is the source (so smoothed it's useless).

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 →
fraction = 0.3;
value = Mathf.Lerp(value, target, 1 - Mathf.Pow(fraction, Time.deltaTime * 10));

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.


Return to overview.