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.
speed
isn't speed, it's a vague quickness.target
is approached, it isn't reached. This moves a vague proportion towards it instead.deltaTime
used, its function here is only a vague improvement.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.
// speed: 10
value = Mathf.Lerp(value, 1, Time.deltaTime * speed);
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));
decay
is 0->∞
. 0
is constant, and larger values approach the target faster.// decay: 10
value = ExponentialDecay(value, target, decay, Time.deltaTime);
// 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));
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.
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.