Search Unity

Call a function on completion of an animation state

Discussion in 'Animation' started by TheCelt, Apr 6, 2016.

  1. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    742
    Hello

    I have a simple animation which fades my canvas group alpha from 1 to 0.

    I was wondering if there was a way to call a function from a script of my choosing once the animation has completed? I couldn't find much documentation on this - but perhaps i don't know the correct terminology for it.

    So i have this :


    Code (CSharp):
    1. public void Test2(){
    2.      animator.SetTrigger("FadeOut");
    3.  
    4. //the general idea:
    5. //if(onComplete == true){ callFunction} ;
    6.  
    7. }
    But i want some way to detect onComplete so i can then call another function.

    Is there anyway to do that?
     
    Last edited: Apr 6, 2016
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    Yes, there's a bunch of different ways.

    The easiest would be to set an animation event on the end of your FadeOut animation. That will call a method when that point is reached.

    Here's the details if you've made the animation in Unity.
    Here's the details if you've imported the animation.
     
    AlcatrazZ_ likes this.
  3. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    742
    I read about this, but then was told if you are experiencing frame drops the event might be missed entirely...i am no technical expert so i don't know how true this is. Do you know what i am referring to as i didn't fully understand what they meant by it but that was why they said to avoid animation events for callbacks that are needed to occur.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    I have never seen an animation event disappear due to dropped frames. I'm pretty sure the system checks all the time between the last frame and the current frame for any event that should have happened.

    What I have seen is animation events disappearing when you're blending between animations. Say you have an animation where a character attacks. You transition out of that animation and into an idle animation. If there's an event on the very end of the animation, that event might not be called due to the blending. I'm not sure that causes this to happen.
     
    ecv80 likes this.
  5. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    ecv80 likes this.
  6. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    742
  7. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    I'm not sure what you mean