Why do nested if statements act differently than the && operator?
I'm following a beginner Unity course, and the instructor used two nested if statements within a method for jumping logic, as so:
//takes care of jumping logic
private void JumpHandler()
{
//store jump input (Jump axis = up/down axis)
float jAxis = Input.GetAxis("Jump");
//if the key has been pressed
if(jAxis > 0)
{
if(!pressedJump)
{
pressedJump = true;
//jumping vector
Vector3 jumpVector = new Vector3(0, jAxis * jumpForce, 0);
//apply force to rigid body
rb.AddForce(jumpVector, ForceMode.VelocityChange);
}
}
else
{
pressedJump = false;
}
}
As I wrote along, I went with:
if(jAxis > 0 && !pressedJump)
as opposed to the instructor's two nested if statements.
For some reason, the two approaches produce different results. The nested if statements cause it to function as wanted, wherein the player hits the spacebar and a vertical force is applied in a uniform way, regardless of how long the spacebar was pressed, the player gameobject jumps to the same height. However, my && statement always reads true as long as the spacebar is held down, causing the player gameobject to fly to the stars. What am I missing here? What's going on?
Answer by Alanisaac · May 11, 2018 at 02:12 AM
It has to do with the else
statement. Let's take your two conditions "jAxis > 0" and "!pressedJump" and just make them "a" and "b" to make things simple. Compare the two situations below. Note how the first statement/comment in the if
block(s) has the same logic, but the else
is different between the two!
if (a)
{
if (b)
{
// this will happen if a and b are true
}
}
else
{
// this will happen if a is not true
}
-----------------------------------------------------------
if (a && b)
{
// this will happen if a and b are true
}
else
{
// this will happen if a is not true OR if b is not true
}
Thanks for the answer! Yes, I understand the difference in the two as you described it, but my issue is still that the first block of code within the if statement(s) runs continuously if I use the && version, but only once with the double-if version. It’s as if my pressJumped boolean never becomes true when I use the && version, and for the life of me I can’t figure out why that is!
It's not that it never becomes true. It's that:
In your original code, the value of pressedJump does not affect whether the else happens
In your modified code (jAxis> 0 && !pressedJump), the value of pressedJump does affect whether the else happens
So let's walk through the logic for entering the JumpHandler multiple times. Let's assume the jump trigger is held down, i.e., jAxis > 0 always:
First time: jAxis > 0 && !pressedJump is true. As a result, pressedJump becomes true and you start jumping
Second time: jAxis > 0 && !pressedJump is now false because pressedJump = true. As a result, you enter the else, and pressedJump becomes false again.
Third time: jAxis > 0 && !pressedJump is true again because pressedJump = false. As a result, pressedJump becomes true and you start jumping more
And on and on. So when the key is held down you bounce back and forth between the "if" and the "else", adding more force every other time you enter the method.
Another thing that might be helpful is that I think your variables aren't named quite right. It sounds like the boolean condition jAxis > 0 is really "pressedJump" and what is "pressedJump" today should really be called "startedJump" or "beganJump" since it doesn't represent the actual keypress.
Eureka, thank you so much! It all clicks now! So basically I don’t want the else statement code to be executed if a is true and b is false, and that’s the key reason for the nested if statement! And the same reason I can’t just use && to achieve the same flow. It just took a night’s rest for it to click with me I guess. Thanks again for taking the time to help me out!
Your answer
Follow this Question
Related Questions
switch case statment 0 Answers
Problem with the logic in my code 0 Answers
I can't make my character move 0 Answers
Simple `Match 3+ in a row` pattern 0 Answers