Search Unity

Help with Jump animation not resetting when jump key held down.

Discussion in '2D' started by Neosythe, Sep 22, 2019.

  1. Neosythe

    Neosythe

    Joined:
    Jan 13, 2018
    Posts:
    2
    Hi there!

    When my player character is in contact with the floor - I have a grounded bool set to true - and the grounded animation is on loop. When the jump key is pressed - grounded is set to false - and the jump animation plays once and then holds on the last frame of the animation. So everything seems to work fine.

    However, when the jump key is held down (or pressed in perfect timing just as the overlap box touches the floor) - the character will jump but the last frame of the jump animation holds and the jump animation isn't played again (even tho, in order to jump, grounded has to == true, which means I should get at least 0.1 seconds of the running animation). Obviously this makes the game look super weird 'cause there is a frozen framed character jumping stop.

    I'll attach the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     private Rigidbody2D myBody;
    8.     private Animator animator;
    9.  
    10.     [SerializeField]
    11.     private float jumpForce;
    12.  
    13.     public Transform platformCheck;
    14.     public LayerMask platformLayer;
    15.  
    16.     private bool grounded;
    17.  
    18.     public Vector3 range;  
    19.    
    20.     void Awake()
    21.     {
    22.         myBody = GetComponent<Rigidbody2D>();
    23.         animator = GetComponent<Animator>();
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         CheckCollisionForJump();
    29.         CheckGrounded();
    30.     }
    31.  
    32.     void CheckCollisionForJump()
    33.     {
    34.         Collider2D bottomHit = Physics2D.OverlapBox(platformCheck.position, range, 0, platformLayer);
    35.  
    36.         if(bottomHit != null)
    37.         {
    38.             grounded = true;
    39.  
    40.             if (bottomHit.gameObject.tag == "Platform" && Input.GetKey(KeyCode.Space))
    41.             {
    42.                 grounded = false;
    43.                 myBody.velocity = new Vector2(myBody.velocity.x, jumpForce);
    44.             }
    45.         }
    46.     }
    47.  
    48.     private void OnDrawGizmosSelected()
    49.     {
    50.         Gizmos.color = Color.red;
    51.         Gizmos.DrawWireCube(platformCheck.position, range);
    52.     }
    53.  
    54.     void CheckGrounded()
    55.     {
    56.         if (grounded == true)
    57.         {
    58.             animator.SetBool("Grounded", true);
    59.         }
    60.         else { animator.SetBool("Grounded", false); }
    61.        
    62.     }
    I also attached a picture of the jumping animation being frozen.

    Anyone got any ideas as to why animations aren't switching - despite the code working as intended?
    If I wait a VERY brief time the animations work fine - there just seems to be a problem with instant jumps for some reason.
     

    Attached Files: