Search Unity

How do you use animation events in Mecanim?

Discussion in 'Scripting' started by Disastercake, Dec 12, 2012.

  1. Disastercake

    Disastercake

    Joined:
    Apr 7, 2012
    Posts:
    317
    I'm having issues trying to understand how to use animation events in mecanim. Is there documentation somewhere I am not seeing? I imagine it has it, since all modern engines actually have animation events (even unity 3.5 had this). If there really aren't animation events, does mecanim offer something similar that can be used with equal ease?
     
  2. harko12

    harko12

    Joined:
    Jul 6, 2012
    Posts:
    10
    If I read their upcoming release notes right, I don't think Mechanim has events yet. You can use curves if you have pro, but other than that I think it's upcoming but not there yet.
     
  3. Disastercake

    Disastercake

    Joined:
    Apr 7, 2012
    Posts:
    317
    Could someone please supply an example for how to use curves as a substitute for animation events? From what I can tell, using curves would be unnecessarily messy since you would have to be checking every possibility in an update function, rather than just waiting for a message to occur.
     
  4. Disastercake

    Disastercake

    Joined:
    Apr 7, 2012
    Posts:
    317
    I'm very interested in getting an answer on this, since without animation events Mecanim just seems like an unfinished product that is useless to most game projects.
     
  5. jwinn

    jwinn

    Joined:
    Sep 1, 2012
    Posts:
    88
    I was looking for this information as well. The only tutorial I can find is 28 minutes into this video, which discusses using curves to change the colliderHeight when jumping: http://video.unity3d.com/video/7362044/unity-40-mecanim-animation

    I figured the only way would be to use a linear curve that reaches the 1 key value where the event occurs, and check for that value in the script. I was just messing around with it for footstep sounds, and I can't get it to work. The value is never 1.0 through code (may be the blending as discussed here). I'm giving up on this for now, until this feature has returned, or someone can shed more light onto a solution.

    In some of the latest blogs, the Unity development team mentions that they are adding animation events and other features. No idea on the ETA of that.
     
  6. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    I'm also waiting for this. Unity Free doesn't even have curves.
     
  7. Disastercake

    Disastercake

    Joined:
    Apr 7, 2012
    Posts:
    317
    Bumping this in case someone in the world knows how we can go about using a clean and effective substitution for animation events. Preferably with a solid example and not just a 2 sentence description.
     
  8. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    There is a stopgap in the Asset Store called "Event System for Mecanim" with a good (single) review. I think I'll wait, personally, but it might steady some others' nerves.
     
  9. Pix10

    Pix10

    Joined:
    Jul 21, 2012
    Posts:
    850
    You can get the normalizedTime of an animation State, and build a your own simple event system around that (I imagine the asset Marble mentions is based around the same call).

    It's nowhere near as flexible as curves, but it's still useful i.e. if you have anim clips that aren't generated by the FBX importer (which is the only way to create Curves on clips atm).

    Responding to events is something else though, since you can't interrupt transitions directly, or animations that are in transition, you have limited flexibility to respond (with animation) to events. Hopefully this will change when we get a more fleshed-out Animator API.
     
  10. jwinn

    jwinn

    Joined:
    Sep 1, 2012
    Posts:
    88
    Thanks, I didn't see that. Here the direct link if anyone needs:
    https://www.assetstore.unity3d.com/#/content/5969

    I'd rather wait as well, but it's been quite a bit of waiting already, so I may give this a try.
     
  11. TokyoDan

    TokyoDan

    Joined:
    Jun 16, 2012
    Posts:
    1,080
    Forget this, I see that the legacy animation system is still available.

    Is it possible to use Unity 4 with the legacy (Unity 3.5.7 animation system) because I need Animation events. Mecanim doesn't have events, it uses "curves" which aren't supported in Unity Basic, which I have.
     
    Last edited: Feb 17, 2013
  12. kanga

    kanga

    Joined:
    Mar 22, 2010
    Posts:
    225
    Yeah I was curious about events, especially for footsteps. Mmmm.
     
  13. joshua-jebadurai

    joshua-jebadurai

    Joined:
    Jul 3, 2009
    Posts:
    14
    I wrote a simple plugin to have events and delegates for Mechanim.

    Check it out at Github

    Code (csharp):
    1.  
    2. AnimatorEvents animatorEvents;
    3.    
    4.     void Awake () {
    5.         // Get the Animator Event Component
    6.         animatorEvents = GetComponent<AnimatorEvents>();
    7.     }
    8.    
    9.     void OnEnable () {
    10.         // Event triggers from AnimatorEvents
    11.         animatorEvents.OnStateChanged += OnStateChanged;
    12.         animatorEvents.OnTransition += OnTransition;
    13.     }
    14.    
    15.     void OnDisable () {
    16.         animatorEvents.OnStateChanged -= OnStateChanged;
    17.         animatorEvents.OnTransition -= OnTransition;
    18.     }
    19.    
    20.     void OnStateChanged (int layer, AnimatorStateInfo previous, AnimatorStateInfo current) {
    21.         // This displays the StateInfo of previous and current states.
    22.         Debug.Log ("State changed from " + previous + " to " + current);
    23.        
    24.         // Animator Events returns a much friendly way than hash names
    25.         Debug.Log ("State changed to " + animatorEvents.layers[layer].GetStateName(current.nameHash));
    26.     }
    27.    
    28.     void OnTransition (int layer, AnimatorTransitionInfo transitionInfo) {
    29.         Debug.Log("Transition from " + animatorEvents.layers[layer].GetTransitionName(transitionInfo.nameHash));
    30.     }
    31.