- Home /
Question by
AndreasX12 · Jul 24, 2013 at 12:44 AM ·
changefloatconvertvalue
Positive to negative not working?
Hi, what I'm trying to achieve is a way to set my car in reverse when my button is pressed / when changeDirection is called.
This is my current code:
bool IsTouchingGearBtn;
IEnumerator changeDirection() {
IsTouchingGearBtn = true;
if(isD == false) {
speed = 0;
speedSliderValue = -speedSliderValue;
isD = true;
} else if(isD == true) {
speed = 0;
speedSliderValue = +speedSliderValue;
isD = false;
}
yield return new WaitForSeconds(0.1f);
IsTouchingGearBtn = false;
}
isD is changing every time I press my button, but I have to press the button 2 times to make it change to a positive/negative value.
Do anyone have any idea of why this could be happening?
Thanks, Andreas.
Comment
Best Answer
Answer by chris_taylor · Jul 24, 2013 at 12:53 AM
speedSliderValue = +speedSliderValue;
that does nothing I would change
if(isD == false) {
speed = 0;
speedSliderValue = -speedSliderValue;
isD = true;
} else if(isD == true) {
speed = 0;
speedSliderValue = +speedSliderValue;
isD = false;
}
To this
speed = 0;
speedSliderValue = -speedSliderValue; //this will get you the opposite so if its -4 it return 4 and if its 4 then it returns -4
isD = !isD // if you still need isD for somewhere else
Thank you so much! I didn't know that -value made it opposite. Thanks alot! :D