- Home /
Trying to stop a float at a certain value.
I have a script that decreases a float using the operator -= and I need it to stop at 0.
if(Input.GetKey(KeyCode.LeftShift) && acceleration <= 20 && sprintDuration >= 1)
{
acceleration = acceleration + sprintSpeed * Time.deltaTime;
sprintDuration -= 2 * Time.deltaTime;
}
else if(acceleration >= 15)
{
acceleration -= sprintSpeed * Time.deltaTime;
}
Answer by SinisterRainbow · Jun 14, 2013 at 04:58 AM
Just check to see when it is
This seems to have worked. I meant to say I need it to stop at 15F. But either way you answered well. I just checked all the values that were decreasing below the number, and if they did I just set them to the number I wanted. So thanks a lot SinisterRainbow!
Answer by Theinsanekiller · Jun 14, 2013 at 04:57 AM
May i know which variable u are talking about. and if you want to stop on 0, then following line of code will work.
if(yourVar>0)
{
acceleration -= sprintSpeed * Time.deltaTime;
}
in the above case instead of yourVar use acceleration.
That is what I kind of have now. Because Time.deltaTime is a decimal number it goes a bit below the value I want, then when it runs by again in makes the statement false, but the number is already wrong. Thanks though for the answer.