Search Unity

Animator trigger doesn't update animation state

Discussion in '2D' started by DoctorWhopper, Oct 11, 2018.

  1. DoctorWhopper

    DoctorWhopper

    Joined:
    Sep 12, 2015
    Posts:
    1
    I am attempting to use triggers to change animation states for a character in game (specifically between the "idle" and "punch" state). Looking through the Unity docs I found a code snippet such as this one

    Code (CSharp):
    1.      //Press the space bar to tell the Animator to trigger the Jump Animation
    2.         if (Input.GetKeyDown(KeyCode.Space))
    3.         {
    4.             m_Animator.SetTrigger("Jump");
    5.         }
    6.  
    7.         //When entering the Jump state in the Animator, output the message in the console
    8.         if (m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Jump"))
    9.         {
    10.             Debug.Log("Jumping");
    11.         }
    In my code, however "if (m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Jump"))" returns false and it still thinks it is in the idle state. I do not have any exit times on the Idle state in the animation state machine controller. Why isn't the trigger switching the AnimatorStateInfo immediately? Thanks :)
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    It could be that the Animator State isn't updated until the next frame. But, that only explains why your if doesn't trigger.

    Have you stepped through the code with the parameter window open, so that you can see if the parameter is triggered?