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

Hi,
I think that you are completely right and it will be fairly clean. Another approach i can think of can be to throw this code into separate script (eg. JumpScript or smth) and disable it from another script when you enter a trigger. Disabled components does not call Update function so there will be no need for if check.