- Home /
(C#) Return Float Variable to previous value
I'm working on finishing up my character (ball) controller, But I've run into a problem. What I'm trying to do is add a brake function to my ball. So, for example, when the "Brake" axis is pressed it will take my speed variable and cut it in half, slowing down the ball. My problem lies when I release the brake button, it doesn't go back to it's originial value, because it has been set to something smaller, so that is it's value now.
So to put it simply, I want to change my Speed variable, then put it back to how it was. Thanks for the help! :)
Answer by Gnometech · Feb 20, 2014 at 03:44 PM
Why not store the original speed and restore it after braking?
bool braking;
float original_speed;
if ((brake_key_pressed) && (!braking))
{
braking = true;
original_speed = speed;
spped /= 2;
}
if ((!brake_key_pressed) && (braking))
{
braking = false;
speed = original_speed;
}
It is a bit pseudo_code, but you should get the meaning. The bool is for ensuring that the cutting in half only happens once when the key is pressed and again to ensure that the restoring also only happens upon release of the brake.
Hope this helps.
Okay, I think I'm getting what your saying. Let me see what I can do with it. Thanks :)
Your answer
Follow this Question
Related Questions
Positive to negative not working? 1 Answer
Change value according to Y position of an object? 2 Answers
Problem With float 2 Answers
Getting a slider value from a different scene 1 Answer
Changing GameObject texture? 4 Answers