Jump animation bool randomly going to false while jumping

Hi All,

I am trying to implement jumping into my simple 2D platformer. The way I am handling the animations is that I’m checking if the player is colliding with the ground. If so, I set my isJumping parameter to false. If he’s not colliding with the ground, I set it to true.

I am running into an issue where about 50% of the time that I jump, my isJumping parameter is setting to true, then immediately back to false. I have messed around with implementations for ~3 hours and can’t seem to figure out why or a way around this issue.

See attached picture for Unity info.

Below is the relevant code snippet for checking for grounding and setting the animation parameter. The jumpPressedRemember and that stuff is to handle coyote time.

void Update() {
        bool bGrounded = Physics2D.OverlapCircle(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
        fGroundedRemember -= Time.deltaTime;
        if (bGrounded) {
            fGroundedRemember = fGroundedRememberTime;
            animator.SetBool("isJumping", false);
        }
        fJumpPressedRemember -= Time.deltaTime;
        if (Input.GetButtonDown("Jump")) {
            fJumpPressedRemember = fJumpPressedRememberTime;
        }
        if (Input.GetButtonUp("Jump")) {
            if (myRB.velocity.y > 0) {
                myRB.velocity = new Vector2(myRB.velocity.x, myRB.velocity.y * fCutJumpHeight);
            }
        }
        if ((fJumpPressedRemember > 0) && (fGroundedRemember > 0)) {
            fJumpPressedRemember = 0;
            fGroundedRemember = 0;
            animator.SetBool("isJumping", true);
            myRB.velocity = new Vector2(myRB.velocity.x, fJumpVelocity);
        }

Since you’re checking if the character is grounded all the time I guess that could be a problem in case you start jumping but before any vertical motion has time to begin a collision could be detected with the ground.
Try adding some sleep-time,

float _lastJumpStart = -1f; //Last time the jump key was pressed
float jumpLaunchTime = .5f;    //Time before first collision check is done
void Update() {
     if( Time.time - _lastJumpStart > jumpLaunchTime) {
         bool bGrounded = Phys...
         if( bGround...
             fGroundedRemember = fGroundedRememberTime;
             animator.SetBool( "isJumping", false );
     }
     ....
     ....
     ....
    if(( fJumpPressedRemember > 0 ) && ( fGroundedRemember > 0 )) {
          _lastJumpStart = Time.time;
          fJumpPressRemember = 0;
          fGroundedRemember = 0;
          animator.SetBool( "isJumping", true );
          myRB.velocity = ......
     }
}

This will stop any collision checks done for some time instantly after pressing the jump key, if this doesn’t work you could try debugging the collision check to see what you collided with, if there could be any self-collision kind of things going on or some invisible geometry