Math question (C#)

I’m having a problem working out how to do this:

I have a float for 0.0 - 100.0 and a speed veriable from 0.0 - 4.0.

I need to find a way to work out the speed from the float in this manner:

50-100 = speed 0.0 - 4.0, so 75 on the float would be 2.0 on the speed.

50-0 = Speed 0.0 - 4.0, so 37.5 on the float would be 1.0 on the speed and 12.5 would be 3.0

I cant think of a way to do this in real time.

hope this makes it clearer:

16363-untitled.png

It’s quite a simple equation actually. First of all, you need to divide the float by 12.5. This gives you a value between 0 and 8. Next, quite simply, you subtract 4 from it. This now gives you a value between -4 and 4. Finally, you can use Mathf.Abs, which is a function that converts any number to its positive counterpart, so Mathf.Abs(1) returns 1, while Mathf.Abs(-3) returns 3.

When it comes to mathematical relations, there is ALWAYS a way to do it, and for something mathematically simple like this, there is ALWAYS a way to do it in real-time.

Just divide by 2.5 in real time ?
100/2.5 = 4.0

@kennyist Use Mathf.InverseLerp(a,b,value) method. gives you the result from 0.0 to 1.0f.
for example mathf.inverslerp(50,100,yourfloat=50) = > result will be 0.0 and your speed will be 0
and Mathf.inverselerp(50,100,yourfloat = 100) => result will be 1.0. and your speed will be 4

and Mathf.inverselerp(50, 0 ,yourfloat = 0) => result will be 1.0. and your speed will be 4
Mathf.inverselerp(50, 0 ,yourfloat = 50) => result will be 0.0. and your speed will be 0

So we can proceed with following checks

if(float >=50)
{
speed = mathf.inverseLerp(50,100) * 4f;
}
else if( float <=50)
{
speed = mathf.inverselerp(50 ,0 ( zero only not hundred) ) * 4f;
}

,Use Mathf.InverseLerp(a,b,value) method. gives you the result from 0.0 to 1.0f.
for example mathf.inverslerp(50,100,yourfloat=50) = > result will be 0.0 and your speed will be 0
and Mathf.inverselerp(50,100,yourfloat = 100) => result will be 1.0. and your speed will be 4

and Mathf.inverselerp(50, 0 ,yourfloat = 0) => result will be 1.0. and your speed will be 4
Mathf.inverselerp(50, 0 ,yourfloat = 50) => result will be 0.0. and your speed will be 0

So we can proceed with following checks

if(float >=50)
{
speed = mathf.inverseLerp(50,100) * 4f;
}
else if( float <=50)
{
speed = mathf.inverselerp(50 ,0 ( zero only not hundred) ) * 4f;
}