Search Unity

Resolved Animation playing even if the condition is not met

Discussion in 'Animation' started by Orkunn, May 10, 2022.

  1. Orkunn

    Orkunn

    Joined:
    Dec 21, 2020
    Posts:
    3
    I have simple project with one character from Mixamo. I setup the animator and it is working fine but it has a issue. The character is transitioning from idle to walk with "w" key and transitioning to run with "w+left shift" key.
    Animator has two boolean parameters called isWalking and isRunning. When I press "w" key it is working as expected and pressing "w+left shift" is working too but as soon as I release the left shift the isRunning parameter is setting to false but the player is still in running animation. Why is that happening?
    upload_2022-5-10_2-14-53.png
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class AnimationStateController : MonoBehaviour
    4. {
    5.     private Animator _animator;
    6.     private static readonly int IsWalking = Animator.StringToHash("isWalking");
    7.     private static readonly int IsRunning = Animator.StringToHash("isRunning");
    8.  
    9.     private void Awake()
    10.     {
    11.         _animator = GetComponent<Animator>();
    12.     }
    13.  
    14.     private void Update()
    15.     {
    16.         var isMoveKeyPressed = Input.GetKey(KeyCode.W);
    17.         _animator.SetBool(IsWalking, isMoveKeyPressed);
    18.         _animator.SetBool(IsRunning, isMoveKeyPressed && Input.GetKey(KeyCode.LeftShift));
    19.     }
    20. }
     
  2. Orkunn

    Orkunn

    Joined:
    Dec 21, 2020
    Posts:
    3
    Silly me I forget to change the transition condition to isRunning=false