Math - How to use a percentage that stays within a range?

Having a brain fart here. So, if I have a min and max value, and I want to use a 0 - 1 float range for 0%-100%. How can I make it so that when I input 50%, I will get right in the middle of the min and max?

Which value ? If it is the tex, I cannot since the tex exists in all cases: if it works, it is the loaded picture, otherwise it is the "?" picture.

1 Answer

1

Something like that?:

//C#
float Map(float min, float max, float t)
{
    return min + t*(max-min);
}

Which is basically what Mathf.Lerp does ^^. “t” should be in range [0-1] and the returned value is in range [min - max].

Example:

float r = Map(3f, 15f, 0.5f);  // --> 9

float r = Mathf.Lerp(3f, 15f, 0.5f);  // --> 9

Just for the record the inverse of that mapping does exist as well: [Mathf.InverseLerp][1] So float r = Mathf.InverseLerp(3f, 15f, 9f); // --> 0.5 It's implemented like this: public static float InverseLerp(float a, float b, float value) { if (a != b) { return Mathf.Clamp01((value - a) / (b - a)); } return 0f; } The "a != b" check is there to prevent a division by zero. [1]: http://docs.unity3d.com/ScriptReference/Mathf.InverseLerp.html

Yes, tex. Íf you move it into the main body of the script you don't have to assign a value to it when declaring it.