Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to wait for an animation to finish ?

Discussion in '2D' started by Orinuvan, Feb 9, 2019.

  1. Orinuvan

    Orinuvan

    Joined:
    Feb 3, 2019
    Posts:
    95
    Guys , as the title suggests , how can I wait for an animation to finish, please answer in the most simplistic way cuz im pretty new to unity , thanks a lot, have a great day :D
     
    edassis likes this.
  2. Fatcat-Games

    Fatcat-Games

    Joined:
    Nov 9, 2016
    Posts:
    34
  3. Orinuvan

    Orinuvan

    Joined:
    Feb 3, 2019
    Posts:
    95
    Thanks buddy
     
  4. gateoffirelive

    gateoffirelive

    Joined:
    Apr 15, 2021
    Posts:
    1

    For AnimatorStateInfo, which property needs to be used to check if the animation has completed one cycle?
     
  5. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    if normalizedtime > 1 then it has completed 1 cycle
     
  6. hotchick

    hotchick

    Joined:
    Apr 6, 2021
    Posts:
    6
    I tried this:
    Code (CSharp):
    1. if (animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1)
    2.   {
    3.             Debug.Log($"Animation over");
    4.         }
    I want to put it in a different method other than Update. It didn't work. Any solution to the problem?
     
    Rpinto30 likes this.
  7. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    you can put in other method but u must decide when u want to call that method
     
    Rpinto30 likes this.
  8. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    You can also just use the animator transition to force it to finish.
    upload_2022-7-8_11-15-47.png

    Check Has Exit Time and then you can Move the 2nd animation to NOT overlap with the first. In the screenshot i share, there is a default overlap. If you move the little arrow handles and then move the bar, it can remove that overlap.
     
  9. Pieter3000

    Pieter3000

    Joined:
    Sep 2, 2020
    Posts:
    3
    What CornySam suggests works, but if you don't want a fancy transition into the next state, you can also do the following:
    • Enable 'Has exit time'
    • Set 'Exit time' to 0
    • Set 'Transition Duration' to 0
    upload_2022-7-9_21-2-41.png
     

    Attached Files:

  10. ComradeVanti

    ComradeVanti

    Joined:
    May 22, 2015
    Posts:
    25
    th2252 and Bshsf_9527 like this.
  11. jmgek

    jmgek

    Joined:
    Dec 9, 2012
    Posts:
    76
    I'm getting some errors with webgl and the browser entering into a background process state (dropping frames), I'm currently polling the state info anyone know the best solution for such an issue?

    Code (CSharp):
    1.  if (animator.GetCurrentAnimatorClipInfo(0)[0].clip.name == name && !animator.IsInTransition(0) && animator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 0.99f)
     
  12. Fenikkel

    Fenikkel

    Joined:
    Sep 24, 2019
    Posts:
    20
    If you have a transition between two states (animations), you need to wait until the transition is done to have the next clip information in th Animator.GetCurrentAnimatorStateInfo(0) and Animator.GetCurrentAnimatorStateInfo(0)

    Do this after a Animator.SetTrigger() to wait for the transition:

    Code (CSharp):
    1.  
    2. // Wait for the transition to end
    3. yield return new WaitUntil(() => _Animator.GetCurrentAnimatorStateInfo(0).normalizedTime <= 1.0f );
    4.  
    5. // Do some action
    6.  
    7. // Wait for the animation to end
    8. yield return new WaitWhile(() => _Animator.GetCurrentAnimatorStateInfo(0).normalizedTime <= 1.0f);
    9.  
    10. // Do some action
    11.  
     
    Last edited: Nov 6, 2022
    lookdad, kokimitsunami and hstoqnov like this.
  13. lookdad

    lookdad

    Joined:
    Jan 10, 2014
    Posts:
    1
    Thanks @Fenikkel! Your post was almost exactly what I wanted, but I did a tiny alteration, because I wanted to know the moment when the animation finished, so that I could trigger something as it started transitioning back to the idle state:

    Code (CSharp):
    1.  
    2. _Animator.SetTrigger("anim_state");
    3. // Wait for the transition to end
    4. yield return new WaitUntil(() => _Animator.GetCurrentAnimatorStateInfo(0).normalizedTime <= 1.0f );
    5.  
    6. // Do some action
    7.  
    8. // Wait for the animation to end
    9. yield return new WaitWhile(() => _Animator.GetCurrentAnimatorStateInfo(0).IsName("anim_state"));
    10.  
    11. // Do some action
    12.  
    (as soon as the state name is no longer the triggered animation, assume the transition back to the idle state)
     
    Fenikkel likes this.
  14. zebman

    zebman

    Joined:
    Dec 22, 2021
    Posts:
    2
    This is the one for me. I try not to use code when the functionality is already there. Thank you. Worked a treat.