What you are looking for is a method which is usually called “map” in other languages. However Mathf doesn’t have such a method. Though what map does is actually the chained result of Mathf.InverseLerp and Mathf.Lerp.
So a reference implementation would be
public static class MapFunctionExtension
{
public static float Map(this float aValue, float aFromMin, float aFromMax, float aToMin, float aToMax)
{
return Mathf.Lerp(aToMin, aToMax, Mathf.InverseLerp(aFromMin, aFromMax, aValue);
}
}
With this extension you can simply do
float speed = 10;
float mapped = speed.Map(0, 90, 5, 25);
This will map the value speed from the range 0 - 90 to the range 5 - 25. So in the case of the value 10 we get back a value of 7.2222
Of course this is just an example implementation. A better solution is to implement the whole mapping in one function since method calls are not for free.
public static class MapFunctionExtension
{
public static float Map(this float aValue, float aFromMin, float aFromMax, float aToMin, float aToMax)
{
// value between 0 and 1
float t = (aValue - aFromMin) / (aFromMax - aFromMin);
// clamp t to the 0-1 range
if (t < 0f) t = 0f;
else if (t > 1f) t = 1f;
// lerp back to the new range
return aToMin * (1-t) + aToMax * (t);
}
}
This would do pretty much the same as the above method but more efficient. Note there are no safety checks when min and max have the same value. Though this should never be the case anyways. Just for reference, this is how Unity implemented InverseLerp, Lerp and Clamp01.