Search Unity

Question How to check if animator animations has finished

Discussion in 'Animation' started by HoboMaggot, Apr 28, 2021.

  1. HoboMaggot

    HoboMaggot

    Joined:
    Jun 10, 2020
    Posts:
    17
    I currently have this issue of properly transitioning one animation to the next after it has finished.
    Im previously used statemachinebehaviour to disable variables from this thread

    This is my script
    Code (CSharp):
    1. if (player_movement.VaderAttack1 == true)
    2.         {
    3.             anim.SetBool("VaderAttack1", true);
    4.             //detects when 1st animation has ended before starting the second
    5.             if (player_movement.VaderAttack2 == true && !anim.GetCurrentAnimatorStateInfo(0).IsName("Vader attack1")) //check when 1st attack anim has finished and 2nd anim has been called
    6.             {
    7.                 anim.SetBool("VaderAttack2", true);
    8.  
    9.                
    10.             }
    11.             else if (player_movement.VaderAttack2 == false && anim.GetCurrentAnimatorStateInfo(0).IsName("Vader attack1") && anim.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1.0f) //check if 2nd anim isnt called and disable 1st anim var after it is finished
    12.             {
    13.                 player_movement.VaderAttack1 = false;
    14.                 anim.SetBool("VaderAttack1", false);
    15.  
    16.             }
    17.  
    18.             if (player_movement.VaderAttack2 == true && !anim.GetCurrentAnimatorStateInfo(0).IsName("Vader attack2")) //disables 2nd anim var after it finishes
    19.             {
    20.                 anim.SetBool("VaderAttack2", false);
    21.                 player_movement.VaderAttack2 = false;
    22.                 player_movement.VaderAttack1 = false;
    23.             }
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    This would be super easy with Animancer (link in my signature) using its End Events.

    I'm not sure exactly what you're trying to achieve, but if you just want to make a simple attack combo system then the Weapons example explains how I would do it.
     
    HoboMaggot likes this.
  3. HoboMaggot

    HoboMaggot

    Joined:
    Jun 10, 2020
    Posts:
    17
    Alright thanks, ill try the end events out.
    I got my own weapon combo working, but the game doesnt want to detect when an animation has finished so Ill keep your tutorial in mind.
     
  4. HoboMaggot

    HoboMaggot

    Joined:
    Jun 10, 2020
    Posts:
    17
    Bump
    is there any way to do this without hooking all my animations to another plugin? Trying to see my options 1st
     
  5. bobadi

    bobadi

    Joined:
    Jan 3, 2019
    Posts:
    674
    if the normalized time of a state is greater than 1, it means the animation has played one full loop
    Code (CSharp):
    1. public Animator animator;
    2. AnimatorStateInfo animStateInfo;
    3. public float NTime;
    4.  
    5. bool animationFinished;
    6.  
    7.  
    8. animStateInfo = animator.GetCurrentAnimatorStateInfo (0);
    9. NTime = animStateInfo.normalizedTime;
    10.  
    11. if(NTime > 1.0f) animationFinished = true;
     
    manbearpigman and artcad like this.
  6. HoboMaggot

    HoboMaggot

    Joined:
    Jun 10, 2020
    Posts:
    17

    1. Now Im just detecting if the animation is running, but when both both the animator bool and normal bool are enabled, while the animation is stuck at the end without transitioning anywhere, the elseif statement doesnt succeed.
    Code (CSharp):
    1.  
    2. else if (anim.GetBool("VaderAttack1") == true && anim.GetCurrentAnimatorStateInfo(0).IsName("Vader Attack1"))
    3.             {
    4.                 player_movement.VaderAttack1 = false;
    5.                 anim.SetBool("VaderAttack1", false);
    6.                 anim.Play("Idle");
    7.  
    8.             }
    9.  
    upload_2021-4-30_13-8-9.png
     
  7. bobadi

    bobadi

    Joined:
    Jan 3, 2019
    Posts:
    674
    this is quite complicated way to do it (and I just realized you already were using ntime>1 in the first post)
    but anyway

    I see you have a different name for state
    in code: Vader Attack1
    in animator: Vader attack1
     
    HoboMaggot likes this.
  8. HoboMaggot

    HoboMaggot

    Joined:
    Jun 10, 2020
    Posts:
    17
    Thanks for pointing out this. Finally fixed it