How can I make a value not go over or below zero?

I am trying to make a 2d character accelerate and then decelerate back to zero. The problem is the movement is dependent on whether or not the x value is positive or negative. For example for the character to move right the x value must be positive and to go left the x value has to be negative.

When decelerating I subtract the values of x so it goes back to zero. The problem is it works when only going to the right where I can put:
if (x<=0)
but how can I make it work for both postive and negative values.

I know this has ben marked as answered, but I just wanted to say this:

You can easily check if the value or field - in your case ‘float’ - is not equal to something with “if(x != 0)” or “if(!x == 0)”. The “!” always means “not”.

In other words, “x != 0” means “x is not equal to 0”. Whenever there is a “!” at the beginning, it means the following is false, so “!x == 0” means “x equals 0 is not true”, which basically means the same, but is told in a different way.

Keep that in mind, it might help you later on.

if(x >= 0)
x = 0;

else if (x <= 0)
x = 0

also known as

x = 0

you dont care if its greater or less you just want it at zero :stuck_out_tongue:

After reading comments from others I think I do finally have a solution to my problem.

if (Input.GetButton(“Horizontal”))
// Accelerating when running
if (Input.GetButton(“Run”))


// Slows down when not pressing buttons

// Clamps the speed to max speed which is for me 5 so it wont go past 5 and below -5
// The main solution to make it stop at zero was rounding it, But it still leaves the problem of sliding at a constant pace for a short period of time.
//antiBumbFactor is just used for going down slopes
moveDirection = Vector3(Mathf.Clamp(Mathf.Round(speed),-maxSpeed,maxSpeed), -antiBumpFactor, 0);

earlier i said this was achievable with Lerp … it is …

void Update()
     x = Mathf.Lerp(x,0,0.1F);

the number x would gradualy come closer to zero (even if negative)
however the closer to zero it gets… the slower it falls to zero … i mean … it goes from -1000 to -1 pretty fast … but after that the fall-rate it begins to slow down dramatically

but there is an even easier way to do this !!!

Edit->Project Settings->Input

choose the axis that you are using to control your character’s x
then set the Gravity to something small … that should do it.
this will make the player input fall slowly to neutral … instead of stopping … possibly creating the desired effect

modulo makes an escalator of numbers.

mathf abs returns the value as positive whether it is negative or positive, and you can also get the

Returns the sign of f.

so that on the one hand you can use the sign as a condition or as a multiplier, and on the other hand you can make a calculation with just a positive number regardless of its sign. so you can do a calculation on the absolute, and then multiply by the sign, and it gives you a lot of control on your game.

If you still can’t understand with all these explanations, you should work at yourself and not look for an answer but test all the options we’ve given you