- Home /
Question by
Rockyblack · Dec 27, 2016 at 04:15 AM ·
javascriptif-statements
Why won't this variable change even when the if part is true?
This code is in the update function. I tested some stuff out and instead of -= or +=, i just did = and it worked. Is there something wrong with the code?
if (Input.touches[0].position.x < 100) {
xSpeed -= 0.3f;
}else if (Input.touches[0].position.x >= 100) {
xSpeed += 0.3f;
}
Comment
Looks legal at a glance, but odd. What exactly is happening? Which parts do or do-not work as expected?
The "else" doesn't look technically illegal but at the very least it reduces readability:
if (whatever) {
// do one thing
}
if (something_else) {
// do something else
}