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 deno$$anonymous$$ator 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 $$anonymous$$, float max, float value) {
if($$anonymous$$athf.Abs(max - $$anonymous$$) < epsilon) return $$anonymous$$;
return (value - $$anonymous$$) / (max - $$anonymous$$);
}
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 $$anonymous$$ and max are the same (deno$$anonymous$$ator is zero) the function should return 0 (or 1, implementation choice), and not $$anonymous$$. (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;
}
Answer by idbrii · Jul 28, 2021 at 07:53 PM
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!).
Answer by Jaakk0S · May 29 at 07:39 AM
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.