Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Animation sometime doesn't play when spamming the animation key

Discussion in 'Animation' started by jimmy50908, Aug 6, 2021.

  1. jimmy50908

    jimmy50908

    Joined:
    Jul 15, 2021
    Posts:
    1
    I have a very simple 2D game. My main character has a walking animation when I press the movement keys. However, if I spam a movement key and then hold it down, it will sometimes not play the walking animation.

    The following is the player controller script that moves and animates the player, though I'm not sure the problem lies here.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     //Variables used for moving the player
    8.     public float moveSpeed = 4f;
    9.     public Vector2 movementDirection;
    10.     public Rigidbody2D rigidBody;
    11.     public float currentMoveSpeed;
    12.     public float horizontal;
    13.     public float vertical;
    14.     public bool isMovingHorizontal;
    15.     public bool isMovingVertical;
    16.     public bool wasMovingVertical;
    17.  
    18.     //Variables used for animation
    19.     public Animator animator;
    20.  
    21.     //Called on start
    22.     void Start()
    23.     {
    24.         rigidBody = GetComponent<Rigidbody2D>();
    25.         animator = GetComponent<Animator>();
    26.     }
    27.  
    28.     //Called every frame
    29.     void Update()
    30.     {
    31.         movementDirection = Vector2.zero;
    32.         movementDirection.x = Input.GetAxisRaw("Horizontal");
    33.         movementDirection.y = Input.GetAxisRaw("Vertical");
    34.     }
    35.  
    36.     private void FixedUpdate()
    37.     {
    38.         MoveandAnimatePlayer();
    39.     }
    40.  
    41.     public void MoveandAnimatePlayer()
    42.     {
    43.         //Test if the user is pressing down any movement keys
    44.         if (movementDirection != Vector2.zero)
    45.         {
    46.             //Move the player
    47.             MovePlayer();
    48.  
    49.             //Animate the player
    50.             animator.SetFloat("Horizontal", movementDirection.x);   //Horizontal Movement
    51.             animator.SetFloat("Vertical", movementDirection.y);   //Vertical Movement
    52.             animator.SetBool("Moving", true);
    53.         }
    54.         else
    55.         {
    56.             animator.SetBool("Moving", false);
    57.         }
    58.     }
    59.  
    60.     //Called whenever the user is pressing down a movement key
    61.     public void MovePlayer()
    62.     {
    63.         currentMoveSpeed = moveSpeed * Time.fixedDeltaTime;     //Set the current move speed (this may be different from moveSpeed when two keys or joystick are held)
    64.  
    65.         horizontal = Input.GetAxisRaw("Horizontal");            //Get the horizontal movement value
    66.         isMovingHorizontal = Mathf.Abs(horizontal) > 0.5f;      //Set if the player is moving horizontally
    67.  
    68.         vertical = Input.GetAxisRaw("Vertical");                //Get the vertical movement value
    69.         isMovingVertical = Mathf.Abs(vertical) > 0.5f;        //Set if the player is moving vertically
    70.  
    71.         if (isMovingHorizontal && isMovingVertical)
    72.         {
    73.             if (wasMovingVertical)
    74.             {
    75.                 movementDirection.x = horizontal;
    76.                 movementDirection.y = 0f;
    77.             }
    78.             else
    79.             {
    80.                 movementDirection.x = 0f;
    81.                 movementDirection.y = vertical;
    82.             }
    83.         }
    84.         else if (isMovingHorizontal)
    85.         {
    86.             movementDirection.x = horizontal;
    87.             movementDirection.y = 0f;
    88.             wasMovingVertical = false;
    89.         }
    90.         else if (isMovingVertical)
    91.         {
    92.             movementDirection.x = 0f;
    93.             movementDirection.y = vertical;
    94.             wasMovingVertical = true;
    95.         }
    96.         rigidBody.MovePosition(rigidBody.position + (movementDirection * moveSpeed * Time.fixedDeltaTime));
    97.     }
    98.  
    99.  

    Any ideas/solutions as to why this happens would be much appreciated.
    Thanks.