This site uses strictly necessary cookies. More Information

X# 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?

**Answer** by orionsyndrome
·
Feb 03, 2019 at 07:47 PM

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.

Oh btw because of this division, you might want to make sure the denominator isn't zero (if for some reason you're implementing this math on your own).

If it's zero, that means the points coincide in space, and thus the line cannot be defined properly.. Therefore, we need a check: if($$anonymous$$athf.Abs(point2 - point1) < epsilon) return point1;

This check is due to floating point precision errors, as you cannot simply check for zero and be absolutely sure that the difference will be exactly zero.

Epsilon is typically a very small value in the order of 1E-5 (that's 10^-5). Or, if you're working with Vector3, you could use the provided kEpsilon.

With this line added, this is exactly what InverseLerp does for you.

```
float InverseLerp(float min, float max, float value) {
if($$anonymous$$athf.Abs(max - min) < epsilon) return min;
return (value - min) / (max - min);
}
```

On the other hand, if the points do coincide, for your implementation you might decide that the result is undefined ins$$anonymous$$d, and throw an error such as ArgumentException (which is a more robust way to handle this in the more general scenario).

Just a note for anyone arriving here via google, the code posted by @orionsyndrome is not quite 100% reflective of Unity's implementation for two reasons:

1) In case where the min and max are the same (denominator is zero) the function should return 0 (or 1, implementation choice), and not min. (I think this is a typo)

2) Unity's InverseLerp clamps the result between 0 and 1.

Unity's implementation looks like this:

```
public static float InverseLerp(float a, float b, float value)
{
if (a != b)
return Clamp01((value - a) / (b - a));
else
return 0.0f;
}
```

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.