How do I use one variable to control the progress of two Mathf.Clamp

I have two variables:

varOne = Mathf.Clamp(progress,0,100)
varTwo = Mathf.Clamp(progress,30,40)

Both with completely different Mathf.Clamp values, but both with the control variable of "Progress". I want progress to be "clamped" between 0 & 100 and to control both my variables (like a percentage).

For example if Progress = 50 then varOne = 50 and varTwo = 35 both variables will be at 50%

1 Answer

1

Try to take a look at lerp: http://unity3d.com/support/documentation/ScriptReference/Mathf.Lerp.html

The code should look something like:

varOne = Mathf.Lerp(0,100,progress/100.0)
varTwo = Mathf.Lerp(30,40,progress/100.0)

Note that Lerp also clamps the value.

Thanks but I'm failing to see how this helps, It works almost the same as my Mathf.Clamp, how can I keep the values between "30,40" but use a controlling value with a range of 0 to 100?

Sorry I think I cracked it, I was using a float as the controlling var, so It appeared to be working the same, realised I needed to change the controlling var to a int.

Strangely that only works as a float 0.0 - 1.0, but none the less it'll do. Cheers

Or you could change all vars to floats

Lerp only interpolates between 0.0 and 1.0 (that's why I divide by 100) in the code example above