Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Jump animation bool randomly going to false while jumping

Discussion in 'Scripting' started by jungle_, Mar 16, 2021.

  1. jungle_

    jungle_

    Joined:
    Sep 13, 2020
    Posts:
    17
    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.

    Code (CSharp):
    1. void Update() {
    2.         bool bGrounded = Physics2D.OverlapCircle(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
    3.         fGroundedRemember -= Time.deltaTime;
    4.         if (bGrounded) {
    5.             fGroundedRemember = fGroundedRememberTime;
    6.             animator.SetBool("isJumping", false);
    7.         }
    8.         fJumpPressedRemember -= Time.deltaTime;
    9.         if (Input.GetButtonDown("Jump")) {
    10.             fJumpPressedRemember = fJumpPressedRememberTime;
    11.         }
    12.         if (Input.GetButtonUp("Jump")) {
    13.             if (myRB.velocity.y > 0) {
    14.                 myRB.velocity = new Vector2(myRB.velocity.x, myRB.velocity.y * fCutJumpHeight);
    15.             }
    16.         }
    17.         if ((fJumpPressedRemember > 0) && (fGroundedRemember > 0)) {
    18.             fJumpPressedRemember = 0;
    19.             fGroundedRemember = 0;
    20.             animator.SetBool("isJumping", true);
    21.             myRB.velocity = new Vector2(myRB.velocity.x, fJumpVelocity);
    22.         }
     

    Attached Files:

    • jump.png
      jump.png
      File size:
      321.1 KB
      Views:
      263
  2. Kona

    Kona

    Joined:
    Oct 13, 2010
    Posts:
    208
    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,
    Code (CSharp):
    1. float _lastJumpStart = -1f; //Last time the jump key was pressed
    2. float jumpLaunchTime = .5f;    //Time before first collision check is done
    3. void Update() {
    4.      if( Time.time - _lastJumpStart > jumpLaunchTime) {
    5.          bool bGrounded = Phys...
    6.          if( bGround...
    7.              fGroundedRemember = fGroundedRememberTime;
    8.              animator.SetBool( "isJumping", false );
    9.      }
    10.      ....
    11.      ....
    12.      ....
    13.     if(( fJumpPressedRemember > 0 ) && ( fGroundedRemember > 0 )) {
    14.           _lastJumpStart = Time.time;
    15.           fJumpPressRemember = 0;
    16.           fGroundedRemember = 0;
    17.           animator.SetBool( "isJumping", true );
    18.           myRB.velocity = ......
    19.      }
    20. }
    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