How can I disable jumping when I hit an object?

Hi guys!

I am creating a Jump & Run game in 2D and I want to disable jumping for a section in the level. I am not sure how I can do it. This is my code for jumping:

void Update()
         {
     
             if (Input.GetButtonDown("Jump") && IsGrounded())
             {
                 characterRb.velocity = new Vector2(characterRb.velocity.x, jumpForce);
             }
     
             if (Input.GetButtonUp("Jump") && characterRb.velocity.y > 0f)
             {
                 characterRb.velocity = new Vector2(characterRb.velocity.x, characterRb.velocity.y * 0.5f);
             }
         }
     
     private bool IsGrounded()
         {
             return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
         }

I think I would use the OnTriggerEnter2D method for hitting the object and include another boolean in the if statement in the update method but that doesn’t sound very clean tho :smiley:

I hope someone can help me out :slight_smile:

Kind regards

Make a bool jumpDisabled, in your jump code check if it is true just like you do with isGrounded, set it to be true when you collide with an object, then reset it when you want them to be able to jump again.