-1

I'm trying to complete a tutorial video about some piece of code that should prevent players to jump continuosly after they die at some point(when they collide with an obstacle).We assigned gameOver to false at the beginning. Wouldn't !gameOver means true? So Wouldn't this code say that [If we press 'Space' and If the player is on the ground and If the gameOver boolean is true, so when the game ends]

The thing is, I didn't understand the idea of putting an exclamation mark on the boolean that has assigned to 'false' at the start to make it 'false'. Also, the tutor preferred the way of putting an exclamation mark on the boolean's left side (!gameOver). He didn't prefer to write it like this:if (Input.GetKeyDown(KeyCode.Space) && isOnGround && gameOver != true)

The tutor's goal is to prevent players keep jumping after they die. I really couldn't understand how this (!gameOver) works. His code:

public bool gameOver = false;
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && isOnGround && !gameOver)
        {
            playerRb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
            isOnGround = false;

            playerAnim.SetTrigger("Jump_trig");
        }
    }
1
  • 1
    !gameOver means completely the same as gameOver == false or gameOver != true. Commented Nov 9, 2023 at 9:02

3 Answers 3

4

The exclamation mark (!) in front of a boolean variable is used to negate its value. In this case, the code !gameOver means “not gameOver”.

So, when the condition !gameOver is true, it means that gameOver is false. This is because !false evaluates to true, and !true evaluates to false.

Thanks.

Sign up to request clarification or add additional context in comments.

Comments

0

The exclamation point on the left side of the variable means "get the opposite of this boolean", which is false if the variable is true, and vice versa. This does not change the value of the variable itself.

Also, the logic of the "if check" seems to be that when the game is in progress and the player is on the ground, when play press space, you want to play the jump animation, change the position of the transforms, and get the player off the ground, so !gameOver should be true.

Comments

0
if (Input.GetKeyDown(KeyCode.Space) && isOnGround && gameOver != true)
{
  // ...
}

gameOver != true is the same as !gameover

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.