Search Unity

GetCurrentAnimatorStateInfo

Discussion in 'Getting Started' started by Deleted User, Dec 23, 2019.

  1. Deleted User

    Deleted User

    Guest

    Hello again. I'll post some code so you can see what's happening...
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if (Input.GetKeyDown(KeyCode.G))
    4.         {
    5.             anim.SetBool("show", true);
    6.             StartCoroutine("StopRunning");
    7.         }
    8.     }
    9.  
    10.     IEnumerator StopRunning()
    11.     {
    12.         // play animation to end then stop
    13.         animationTime = anim.GetCurrentAnimatorStateInfo(0).length;
    14.  
    15.         if (anim.GetCurrentAnimatorStateInfo(0).IsName("BarrelS_Hide"))
    16.         {
    17.             Debug.Log("No");
    18.         }
    19.         else if (anim.GetCurrentAnimatorStateInfo(0).IsName("BarrelS_Run"))
    20.         {
    21.             Debug.Log("Yes");
    22.         }
    23.         else
    24.         {
    25.             Debug.Log("Couldn't find name");
    26.         }
    anim.SetBool makes BarrelS_Run play in-game, but has only taken the .length from BarrelS_Hide. I placed get_length in the Coroutine so the animator had time to figure out it was off the last animation. I know there are transistions between animations, but if Run is being played, surely animationTime should equal the length of BarrelS_Run?
    Debug.Log shows "No"
     
  2. Deleted User

    Deleted User

    Guest