What exactly does Mathf.InverseLerp() do?

Example Mathf.InverseLerp(5.0f, 10.0f, 8.0f) this return 0.6, whats mean this value returned?

Unity doc say me that this value is inear parameter t that produces the interpolant value within the range [a, b]. What does that mean?

it’s saying that 8 is 60% of the way from 5 to 10.

If you need to find how far the value is in respect with two fixed values (obviously as a ratio, which can be thought of as a percentage), you need a Lerp, which is short for ‘linear interpolation’.

Simply put, if you think about (i.e.) point1 at 0% and some other point2 at 100%, there is a simple way to find any other point in between these two if you imagine a line between them.

If you think about it, you can express all points on this line as point1 + offset. If your offset is 0, then point1 + 0 and you’re exactly at point1. If your offset is equal to difference, where difference = (point2 - point1), then you arrive at point2 (because point1 + (point2 - point1)). Conveniently, you can introduce a multiplier to this difference, and so you come up with a formula point1 + factor x (point2 - point1) where a value of 0 gives you point1, a value of 1 gives you point2, and 0.5 (or 50%), gives you exactly the midpoint between the two.

This is what Lerp does.

However, sometimes we need to do the opposite, we need that multiplier when we only had a concrete value. For this we solve this equation backwards.

value = point1 + factor x (point2 - point1), where value is known, but the factor is unknown
factor x (point2 - point1) = value - point1, now we divide the whole thing by (point2 - point1)
factor = (value - point1) / (point2 - point1)

And this is what InverseLerp does.

InverseLerp finds the progress between two values.

For InverseLerp(float a, float b, float value), a is the start, b is the end, value is the current position. It returns how far along the “path” value is from a to b.

For example, you might use it in a flocking algorithm to apply falloff in your Seek behaviour: var seek_influence = InverseLerp(min_distance, max_distance, current_distance) Now you know how strongly you should apply your seek behaviour (0 = don’t apply, 1 = maximum power!).

This is the most common use case for InverseLerp in Unity:

“When param A goes from 45 to 145 I want param B to go from 30 to 10”.

B = Mathf.Lerp(30f, 10f, Mathf.InverseLerp(45f, 145f, A));

InverseLerp will return a range 0…1 which is perfect input for Lerp. Then again it’s perfect input for much anything, such as a probability for something happening.