Search Unity

Animation event not triggering

Discussion in 'Animation' started by Happy_Jingle, Feb 13, 2017.

  1. Happy_Jingle

    Happy_Jingle

    Joined:
    Mar 9, 2014
    Posts:
    103
    Currently in my game a lot of events are triggered from within an animation and occasionally they seem to be skipped over. It's imperative that an event never get skipped, how do I make sure this doesn't happen?
    Here are the settings on all my animations:
    Example of how events are placed:
     
    MysterieCatGames likes this.
  2. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
  3. Happy_Jingle

    Happy_Jingle

    Joined:
    Mar 9, 2014
    Posts:
    103
    The answers on a lot of the threads I've gone through are quite old, I was hoping there was updated information. Furthermore they aren't actually "answers" more like "this is broken don't use it" so I was hoping to find an actual answer.
     
    stoppo, Yanza, pjbaron and 20 others like this.
  4. omegabytestudio

    omegabytestudio

    Joined:
    Nov 9, 2015
    Posts:
    77
    @Happy_Jingle

    I feel you man.

    I might be able to help you, not sure if it's the same issue.

    I had trouble when I tried to call an event on a frame, and that frame was occuring during a transition to another animation.

    Like If I was punching, and mid animation I would kick, the last frame wouldn't call the event.

    Could it be your issue too?

    To resolve this issue, I'm using a way that might not be the best, but it works for me.

    I have both event on the animator and state machine behavior with the same event call on the OnStateExit fonction.

    Why both?

    Event : Sometime won't call on transition.

    State Machine Behavior : Won't call OnStateExit if the animation loop, and it was looping for me. If you aren't looping, you can only use State Machine Behavior.

    I hope it helps you!
     
  5. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Please log a bug if there is something that doesn't work as expected.
    We do have over 50 different test for animation events that are run everytime we do build unity that cover many edge case.

    If there is some case that still doesn't work we would like to know
     
    OfficialHermie likes this.
  6. omegabytestudio

    omegabytestudio

    Joined:
    Nov 9, 2015
    Posts:
    77
    Does the situation I describe is a bug? I just thought it was how it was supose to work?
     
  7. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    If you expect that your animation events should be fired but it not fired then yes log a bug, we will do the investigation and if we found out that it broken we gonna fix it, if it not broken we will give you an explanation why we think it work as expected.

    So yes log a bug
     
    OfficialHermie and theANMATOR2b like this.
  8. Ylisar

    Ylisar

    Joined:
    Jan 27, 2014
    Posts:
    19
    Where is the documentation for animation events with mecanim? I can only seem to find the docs for animation events when using the legacy system. For example, how does events work during a transition? Some googling seems to suggest they're broken with mecanim, but those are mostly old posts.
     
    Deeeds likes this.
  9. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    There is nothing specific to mecanim or legacy, basically if a clip is currently playing it should trigger events.

    A transition is essentially a cross fade between 2 animation clip, so as long as both clip are played they should both trigger animation events.

    There is no know issue with animation events at the moment in 2017.1 and on newer version so I would expect them to work reliably
     
    Ylisar likes this.
  10. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    Last frames on animations are definently skipped at times. Especially during a transition to another anim. Giving me headaches
     
  11. Bitboys

    Bitboys

    Joined:
    Apr 22, 2015
    Posts:
    156
    Any news about how to solve this?
    For me it is impossible to use animation events because sometimes they work and sometimes they do not.
    I just want to send a message to another script when the animation reaches a certain frame but it does not work exactly. It's a simple trigger animation.
     
    Deeeds likes this.
  12. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Please log a bug, there is currently no know issue about animation events not firing for the animator component
     
  13. ed7k

    ed7k

    Joined:
    Jul 4, 2016
    Posts:
    1
  14. SergioMartinPulido

    SergioMartinPulido

    Joined:
    Mar 19, 2018
    Posts:
    1
    Hello I am having the same problem. I will introduce my case:
    I am using Animation Events to trigger sounds at a specific frame. For some reasons the events placed at the first key frame are only triggered the first time I play the animation. The next times I try to play the animation the Animation Event is skipped. I am using Simple Animation Component to trigger the animations not Mecanim. (So we are not using transition blending or exit time)

    https://blogs.unity3d.com/es/2017/11/28/introducing-the-simple-animation-component/

    In theory uses Playables to play the animations.
    My unity version is 2017.3.1f1

    Any help will be very usefull

    Regards

    trunk (Ejecución) - Microsoft Visual Studio .jpg
    PrtScr capture_3.jpg

    Animation event not triggering  Unity Community - Google Chrome.jpg
     
    Deeeds likes this.
  15. willianbrasil

    willianbrasil

    Joined:
    Oct 17, 2017
    Posts:
    5
    Hi i've worked on three different projects with Unity and i've always had problems with animation events at the end of animation because they seem to fire whenever they want to, to solve this i've come up with two methods:

    1: Put a phantom frame at the end of the animation, after the event frame, and example of phantom frame is a GameObject property which will be enabled/disabled according to your current animation so it won't change anything.

    2: Get the animation length which you desire to activate the event and at the end of the desired animation activate the event.

    Obs: I've never used state machine behaviours, so it might be a good alternative.
     
    literacy likes this.
  16. NathanJSmith

    NathanJSmith

    Joined:
    May 11, 2018
    Posts:
    57
    If Blend Animation causes Animation Event unreliable, then I have to drop Animation Event method. I can use StateMachineBehaviour & Animator.GetCurrentAnimatorStateInfo.normalizedTime to handle the event myself for this case (cause I can't drop Blend Animation for this case)
     
  17. coute

    coute

    Joined:
    Jul 26, 2014
    Posts:
    9
    I'm having this problem as well. I'm using Animation Events to tell my Behavior Designer tree when an animation has finished playing (the animation event fires on the last frame of the animation). In the tree, it just waits forever until it gets the message that the animation has finished. It works most of the time, but sometimes I would find an enemy who is in the next crossfaded animation state but whose behavior tree is still waiting for the animation end event-fired message for the previous animation.

    Nathan's workaround sounds like a right pain in the ass. I have a lot of events besides the end of animation event so any news on a permanent fix for this would be appreciated.

    EDIT: Just to be clear, the animations I'm putting the animation end event on are not looping. I've tried other solutions for telling when an animation has ended but those had problems as well.
     
    Last edited: Nov 18, 2018
    literacy likes this.
  18. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,568
    It's been a while since I had to use Mecanim, but could it be that the transition is starting before the end of the animation and timed to finish at the end of the animation? In that case, floating point error between the keyframe/event and the system that determines which states are playing could be causing the state to stop before it actually evaluates the last frame. That would explain why it only happens some of the time since it would be precision errors accumulated from the varying delta time.
     
  19. RLord321

    RLord321

    Joined:
    Feb 25, 2017
    Posts:
    28
    I had the same issue...but I fixed it. I had an trigger at the end of the animation frame and it was firing based on debugging but my code was not handing my characters different action properly. For example, I had the trigger call a function when the animation ended to indicate the attack was done so the AI could do something else...like attack again. To make a long story short, I believe it may have been continuing an animation before it actually stopped and maybe starting again? Not sure but when I started the animation specifically starting at frame 0, it works fine:

    anim.Play( "attack", -1, 0);

    I hope this helps!
     
  20. tataygames

    tataygames

    Joined:
    Aug 4, 2016
    Posts:
    55
    Tried it on unity 5.5.6 and the last frame with trigger event is still skipping.


     
  21. coute

    coute

    Joined:
    Jul 26, 2014
    Posts:
    9
    I'm using CrossFade so that's not going to work. I still have this issue and it's really annoying.

     
  22. OfficialHermie

    OfficialHermie

    Joined:
    Oct 12, 2012
    Posts:
    585
    @Mecanim-Dev I have filed a bug report (Case 1113342) in December that deals with the same issue.
    I struggled a bit to create a project / sample scene that would make it most easy for the one who handles this report to follow / see the bug.
    Unfortunately I filed a difficult-to-follow bug report (Case 1113170) before that.
    In Case 1113342 I explained that this new case is an improved bug report of Case 1113170 and that Case 1113170 can be closed.
    Case 1113342 is still open, and Case 1113170 is closed.
    This is perfectly fine, but I wonder what the status of Case 1113342 is since it appears open and not "under investigation" or so. I'm afraid this case is somehow forgotten due to my "double posting".
    Do you think you could perhaps check if Case 1113342 is still under investigation?
    Thank you very much!
    (Sorry for the difficult explanation!)
     
  23. sbsmith

    sbsmith

    Joined:
    Feb 7, 2013
    Posts:
    126
    We are looking at this right now (2018.4.7). Our Animator is set to Animate Physics, but then it will miss events. If we switch it back to Normal, it will hit the events. Since we need it to run on Animate Physics (due to root motion and custom physics), we need to figure this out.
     
    Last edited: Sep 13, 2019
  24. bakelord

    bakelord

    Joined:
    Jan 19, 2014
    Posts:
    8
    We ran into an issue today where an event is missed on frame 0 (and the first few frames after that) but only if the Mirrored parameter is set to true on the same frame that the animation is played. The animation is driving a humanoid rig, hence the mirror flag. It's very odd and simply commenting out Animator.SetBool("Mirrored", true)* ensures that the event is fired.

    [edit] I forgot to add, our animator is disabled and we advanced it manually, so when we kick an anim it looks like so:
    Animator.Play(animName);
    Animator.Update(0);
    I appreciate this probably not a common usage scenario. :)

    * "Mirrored" is the parameter that is hooked up to the Mirror setting in our animator states.
     
    Last edited: Oct 9, 2019
  25. WigglingPotatoDev

    WigglingPotatoDev

    Joined:
    Sep 13, 2017
    Posts:
    3
    I ran into a related issue where animation events weren't hitting my event handlers. My Animator was set to Normal for Update Mode, and switching it to Animate Physics fixed it.
     
  26. WigglingPotatoDev

    WigglingPotatoDev

    Joined:
    Sep 13, 2017
    Posts:
    3
    Never mind. Changing the Update Mode setting has no effect. Something is screwy because my event triggers are working regardless of that setting. I have no idea why it's working again.
     
  27. ebrewe

    ebrewe

    Joined:
    Jan 9, 2015
    Posts:
    2
    If you know the answer, give the answer. Maybe op searched 50 threads and just didn't happen to see the right one. If you put the answer on this forum, now it's in two places and easier for people to find in the future. Why you gotta be a dick about it?
     
  28. zcyonzcyon

    zcyonzcyon

    Joined:
    Jul 10, 2018
    Posts:
    1
    If you're here looking for an answer (talking to whoever stumbles upon this thread), in my case it was that I had an animation transitioning to "Exit" without any conditions, and the exit time was set to a value lower than 1 (0.90 was the value, to be specific), effectively missing the events that were in that 0.10 of the animation that "wasn't being played".

    I hope it helps.
     
    literacy and M_R_M like this.
  29. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,568
  30. eliteforcevn

    eliteforcevn

    Joined:
    Oct 25, 2018
    Posts:
    47
    It still the problem in 2021 i put an event on a frame before transition blend between 2 animation but some time the event is not called it passed but not call i dont know :D you should let all events on animation be called even it on transition blend frame :D
     
    Noxalus likes this.
  31. hieutran196

    hieutran196

    Joined:
    Dec 17, 2018
    Posts:
    21
    Have you solved it.
    In 2019.4.24. Event still doesn't trigger at near end of frame.
     

    Attached Files:

  32. grossimatte

    grossimatte

    Joined:
    Mar 15, 2013
    Posts:
    43
    This is one of those ridiculous things Unity will never solve.
     
    rell023 and DonCornholio like this.
  33. Tentakero

    Tentakero

    Joined:
    Mar 5, 2017
    Posts:
    17
    This solved it for me. I had dragged the fade time down to 0 and it was chopping off part of the end animation. Bringing it up to include the full bar of the animation allowed the trigger to play normally.
     
  34. hazard7

    hazard7

    Joined:
    Apr 10, 2021
    Posts:
    1
    For me, On the editor it works fine but on Android the events are not consistently being fired.
     
    Last edited: May 31, 2021
  35. Skotrap7

    Skotrap7

    Joined:
    May 24, 2018
    Posts:
    125
    Just found this today. Have you been able to figure out how to get animation events to work on Android? I'm noticing the same thing. Events work fine in editor, and regular Mac/PC builds, but not on Android.

    Starting to feel like Android support is being left behind.
     
  36. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    130
    Last-frame events are indeed still being skipped as of July 2021. I just ran into this issue with a game I'm building for Android.
     
  37. k_dunlop

    k_dunlop

    Joined:
    May 10, 2019
    Posts:
    16
    I had the same issue but Tentakero's solution fixed it.
    if you drop an AnimationEvent an event at the end frame of the animation (or poss at the very beginning) it will not fire.
     
  38. TallPaulUK

    TallPaulUK

    Joined:
    Jul 27, 2013
    Posts:
    7
    As others have noted this issue seems to happen when you have a transition between one state and another, To fix set the transition duration to 0 or put the event before or after the transition.
     
  39. OhiraKyou

    OhiraKyou

    Joined:
    Mar 27, 2012
    Posts:
    259
    Animation events, in my experience, have been the most common source of AI bugs. As such, I suggest using a custom timer script that's entirely independent of the system. Here are some guidelines:
    • Start timers when you change animation states.
    • Use seek position (0-1) to keep track of and evaluate the time, to scale relative time markers with duration.
    • As you update the time, invoke UnityEvents that the current time has passed over since the last update.
    • If you need remaining events to always run on state change, update the current time seek position to 1 or 0 (the end, based on seek direction—forward or backward) to set the current time to its duration and, consequently, invoke events that remain. Do this by calling a set time position function from state change UnityEvents outside of the timer system, because you'll need to decide when to do it based on the specific event (e.g., not doing it when an enemy has been destroyed).
    • Implement togglable looping and seek position wrapping (when greater than 1 or less than 0) and invoking events passed when seeking backward (negative delta time), if desired.
    • Consider invoking additional, separate events when passing time markers backward and forward, specifically, to enable separate cleanup operations and event orders.
    • Invoke additional, separate events on overall timer and individual loop start and stop.
    Ideally, the animation system itself should expose functionality to reliably handle this universally necessary stuff. But, seeing as it doesn't, a custom timer is a serviceable stopgap solution.
     
    Last edited: Sep 9, 2021
    ekawano114 likes this.
  40. Kinfinite

    Kinfinite

    Joined:
    Dec 9, 2019
    Posts:
    5
    Thank you so much for this, I was looking all over for a solution for an issue I was having and could not for the life of me find something. Honestly I wasn't even sure I knew what the issue was.
     
  41. Pawciu

    Pawciu

    Joined:
    Jun 21, 2015
    Posts:
    16
    For me it was fixed when i ensured that animation transition occurs after last frame is reached(exit time set to length of the first animation)

    upload_2021-11-25_16-23-14.png
     
    literacy, SuspecM and theNfan like this.
  42. theNfan

    theNfan

    Joined:
    Dec 30, 2020
    Posts:
    25
    Thanks, that actually helps!
     
  43. siliconkgk

    siliconkgk

    Joined:
    Oct 5, 2021
    Posts:
    27
    Its pretty crazy how Unity still has no easy way to just tie an event to absolutely happen when the animation is stopped by saying, if this animation clip is done or transitioned to another animation, regardless of the 100 different ways to set up a state machine, ect, just simply call an event when the animation is done or interrupted. Unity is tying the event to the animation timeline of key frames, there should be an easy on start and on stop or transition to call a function. Were playing timing games with stuff here. There shouldn't be 10 different ways to screw up how calling an event tied to the animation works based on the rest of the animation system. Onstart,OnStop needed......
     
    Last edited: May 2, 2022
    JohnnyConnor and literacy like this.
  44. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,723
    It's not really. This is the attitude they had towards animation for many years. They had the old animation system, which they wanted to retire. They purchased / hired / whatever motionbuilder like tech / people and made the animator. The animator was supposed to be a complete replacement, even though initially it was only good for complex humanoids and nothing else.

    Eventually development hit a brick wall. Years later we got the feature of being able to zoom in and out on the animator state machine, and that's about all the useful improvements animator got. In the meantime, semi-unofficially they started suggesting people use the legacy animation again.

    Playables got introduced and then abandoned (and was never... good).

    Timeline got introduced and many years later it's still pretty bad.

    They are preparing new DOTS related tools, but they won't be ready in time for DOTS 1.0.
     
    ethanicus and siliconkgk like this.
  45. Splosions

    Splosions

    Joined:
    Apr 29, 2017
    Posts:
    30
    Still an issue.
    Giving the animation an additional 0.3s of "Ghost frames" after the animation is complete seems to do the trick so far
     
  46. DaveA_VR

    DaveA_VR

    Joined:
    May 26, 2022
    Posts:
    35
    Using Unity 2021.3.2. I have a set of animations I load from asset bundles and play. I need to know when they end. I would expect events to emit from the Animator, but hey.
    So in code, I add an event at the end to fire. Almost never fires. Yes, I do sometimes have transitions to other animations running. Seems like only the last animation get the best results (it has no exit transition).

    Code (CSharp):
    1. ...
    2.             AnimationEvent ae1 = new AnimationEvent();
    3.             ae1.functionName = "TellTheTime";
    4.             ae1.time = clipToTransitionTo.length;
    5.             ae1.floatParameter = 0f;
    6.             AnimationEvent ae2 = new AnimationEvent();
    7.             ae2.functionName = "TellTheTime";
    8.             ae2.time = clipToTransitionTo.length - 0.1f;
    9.             ae2.floatParameter = 0.1f;
    10.             AnimationEvent ae3 = new AnimationEvent();
    11.             ae3.functionName = "TellTheTime";
    12.             ae3.time = clipToTransitionTo.length - 0.025f;
    13.             ae3.floatParameter = 0.025f;
    14.             AnimationEvent ae4 = new AnimationEvent();
    15.             ae4.functionName = "TellTheTime";
    16.             ae4.time = clipToTransitionTo.length - 0.2f;
    17.             ae4.floatParameter = 0.2f;
    18.  
    19.             clipToTransitionTo.AddEvent(ae1);
    20.             clipToTransitionTo.AddEvent(ae2);
    21.             clipToTransitionTo.AddEvent(ae3);
    22.             clipToTransitionTo.AddEvent(ae4);
    23. ...
    24.  
    25.         private void TellTheTime(float tick)
    26.         {
    27.             Debug.Log($"%%% Time at t-{tick} is {Time.time}");
    28.         }
    29.  
    Note: I am adding events to fire at the end, .1 seconds before end, .025 seconds before end, and .2 seconds before end.

    I would expect all of these events to fire, as the animation is longer than .2 seconds.
    Most of the time, none of them fire. The exit transition time is 1 second. I'm betting if I put an event out that far I'd see it.

    So this is still a bug in 2021.3.2 at least. For me to create a real bug report would take precious time out of my day I cannot currently afford.
     
  47. DaveA_VR

    DaveA_VR

    Joined:
    May 26, 2022
    Posts:
    35
    Dear Unity:

    Please add these events. Or let us know why you can't:

    Animator
    Messages:

    OnStarted (clip, or clip name)
    OnPaused(clip, or clip name)
    OnResumed(clip, or clip name)
    OnStopped(clip, or clip name)

    AudioSource
    Messages:

    OnStarted (clip, or clip name)
    OnPaused(clip, or clip name)
    OnResumed(clip, or clip name)
    OnStopped(clip, or clip name)

    Thank you
     
    literacy and vwork like this.
  48. Skotrap7

    Skotrap7

    Joined:
    May 24, 2018
    Posts:
    125
    Just use Animancer. It's a much better animation system. Plus, Unity probably won't be adding much more support for the existing animation system since they are going to be building a Motion Matching system to replace it.
     
    eliteforcevn likes this.
  49. pjbaron

    pjbaron

    Joined:
    Jan 12, 2017
    Posts:
    53
    We've just reworked a fairly large project to use Animation Events because they (ought to) tie together multiple systems very tightly despite our build target having frequently dropped individual frames (we're building for a service that generates dynamic interactive video and streams it).
    I had assumed that Unity would trigger events even in the middle of the skipped period (e.g. if nextEvent.time <= Time.time), but given the current intermittent problems where entire sequences of special effects are not being fired, I am starting to think they've done something really dumb like assuming that there will never be a dropped frame (e.g. if animation.frame == event.frame)
    Deadline is Friday... um...
     
    ChristopherBKroll likes this.
  50. valentin56610

    valentin56610

    Joined:
    Jan 22, 2019
    Posts:
    156
    January 2023, Unity 2021.3.16f1, last frame events ARE skipped......

    EDIT: Of course I have no exit time or anything like that that would delay the call, all set to 0.