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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to know when animation finished? Mecanim

Discussion in 'Animation' started by pretender, Jun 20, 2015.

  1. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    Hi I am trying to figure out when animation ended so that I can trigger an event. I am currently using events on animations but it isn't very reliable and you can't simple put marker on very start or end of animation, it simply won't trigger. What I have to do is to move marker forward from start animation or move marker back from end of animation to get it to work. I suspect that has something to do with transitions between animations but it simply isn't reliable. There has to be a way to get a clip length or something like and to monitor when it's ended?

    Thanks for your help!
     
    Last edited: Jun 20, 2015
  2. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    698
    you could do something like this ..

    Code (JavaScript):
    1.  
    2.  
    3.  
    4. while(currentBaseState.nameHash == WalkState)
    5. {
    6.     // wait until the animation has finished
    7.     yield;
    8. }
     
  3. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,614
    theANMATOR2b likes this.
  4. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    You should log a bug if animation events are not reliable.
    In 5.1 we did rework on AnimationEvent and we did add a lot of runtime test to validate animation event reliability. It should work.
     
    Griffo likes this.
  5. neno1204

    neno1204

    Joined:
    Jun 23, 2015
    Posts:
    14
    But stateBehaviour gives the same problem as events. Firing second animation right after first causes second animation OnStateEnter to be called before first animation OnStateExit (screenshot). I guess this happens due to Unity’s animations transition and their blending. However the Griffo’s solution would work.

    animations_screenshot.png
     
  6. asd888

    asd888

    Joined:
    Feb 13, 2015
    Posts:
    2
    when i builded a 3d-object prefab into an assetbundle which had "StateMachineBehaviour" attached to it's animator states with unity5.0.3,
    then i load that assetbundle, but the "StateMachineBehaviour" script was not excuted....
     
  7. Rico21745

    Rico21745

    Joined:
    Apr 25, 2012
    Posts:
    409
    I am experiencing this same issue as well. It always happens when I have one animation state with the same behavior, with a transition to another animation state with the same behavior.

    Just like you I believe this is to do with Mecanim blending the animations. However the expected behavior imho is that anim1 OnStateExit should be called before anim2 OnStateEnter is called. This is not the case as of the latest version of Unity as far as I can tell.
     
  8. neno1204

    neno1204

    Joined:
    Jun 23, 2015
    Posts:
    14
    I am still dealing with this problem and I opened new thread..
    http://forum.unity3d.com/threads/state-machine-behavior-onstateenter-and-onstateexit-problem.340131/
     
  9. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,345
    How to use this? I need specific example code....that works.