(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! :slight_smile:

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.