- Home /
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) x=0 but how can I make it work for both postive and negative values.
$$anonymous$$athf.Clamp won't help because the speed needs to slowly decrease so the player will slightly move slower when slowing down. Also the x values will go past zero since it is going to increase in speed.
Can you explain how Lerp would be useful? I only know that lerp can be used to find an average between 2 numbers but thats it.
Answer by mkjrfan · Nov 22, 2012 at 04:17 PM
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")) { speed+=(((walkSpeed*acceleration))*Time.deltaTime*4)*Input.GetAxis("Horizontal"); }
else { speed=(walkSpeed)*Input.GetAxis("Horizontal"); }
} // Slows down when not pressing buttons speed-=(speed*declerate)*Time.deltaTime;
// 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);
Answer by LukaKotar · Nov 24, 2012 at 08:38 AM
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.
Thank you for your comment. I did know that ! means not beforehand , but I think you're right and it will come in handy later on.
please note that for small variances in value float comparisions are unreliable.
float comparisons are actually approximated to compare.
Answer by sparkzbarca · Nov 21, 2012 at 03:09 AM
if(x >= 0) x = 0;
else if (x <= 0) x = 0
also known as
if(decelerating) x = 0
you dont care if its greater or less you just want it at zero :P
if you want it to be gradual in slowing
deceleration_modifer = .5;
x = x * deceleration_modifer;
if your at -100 it will be
-100 * .5 = -50
if your at 100 it will be
100 * .5 = 50
might want to add a
if (mathf.approximately(x, 0) x = 0;
that way once its really close to 0 it becomes 0
The thing is it won't work when moving in an opposite direction of the x value. For example if the player moves left the x value will be less than zero, so by putting if(x<0) x=0; then the character cannot move left because the x value will make it zero since x will be a negative value in order for it to move to the left.
I have tried this already and that is the result.
oh your testing even during acceleration?
anyways my comment answers your question.
dont subtract. $$anonymous$$ultiply so you keep the sign.
The whole process I'm trying to achieve is slowly gaining up speed when the running button is pushed and then when nothing is pressed that is when the character would decelerate.
I tried the x=x*deceleration_modifer (which is for me speed= speed*acceleration) and it might also be where I placed the deceleration as well. Should I post what I did?
acceleration = 2;
deceleration = .5;
if (run_button_down)
{
speed = speed * acceleratoin;
}
if (run_button_up)
{
speed = speed * decleration;
}
if speed is right so positive
speed = 10;
speed = 10 * .5;
speed = 5;
if speed is left so negative
speed = -10
speed = -10 * .5
speed = -5;
how is this not what you want?
either way positive or negative the number approaches zero
Answer by IT_Criminal · Nov 23, 2012 at 01:01 PM
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
What does .1F mean? Yes I think I am starting to understand how Lerp would help.
ok ... let me explain
in c# you have to put "F" after floats or the compiler won't detect them properly ... that 0.1F is actualy 0.1 ... did you try the input alteration approach ?
No I did not but I will try and test it later on. Thank you for your help.
np ... altering control sensitivity/gravity to create acceleration/deceleration seems the best solution to me because it doesn't involve any code
Answer by MountDoomTeam · Nov 24, 2012 at 08:06 AM
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
Mathf.Sign 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
If you looked at all the answers you would have found out I did find an answer by myself by using all of the help given to me.