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

Animation events not always firing? (Mecanim)

Discussion in 'Animation' started by ChrisSch, Aug 31, 2016.

  1. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    Hi, so I'm having issues with animation events not always firing. Half the time actually, or worse.

    I have attack animations, and I added a function event at the beginning of the swing, and at the end of the swing on the attack animation.

    The event calls this function:
    Code (CSharp):
    1. void AttackSwitch()
    2.     {
    3.         foreach(Weapon_2 weapon in weaponScripts)
    4.         {
    5.             if(!weapon.attacking)
    6.                 weapon.attacking = true;
    7.             else
    8.                 weapon.attacking = false;
    9.         }
    10.     }
    It basically tells the each event if its attacking or not so the trigger on the weapon can deal damage to whatever its touching only while attacking is true.

    The problem is half the time those events don't fire at all. I read somewhere that events aren't being called during a transition, (eg. idle >> attack), and as much as I dislike that, I made sure the transitions aren't overlapping the event frames but its still happening half the time. Also read they might be skipped if the frame rate is low, but not sure about that. My frame rate is fine anyway.

    I tried using curves too. I set the flat to 1, then beginning of attack to 1, middle to 20, end back to 1, and have it constantly set "attacking" to true while the curve is more than 1, so that's every frame, but even so it doesn't fire every time. And that's during the same idle to attack transition. Even if I wait a couple seconds after the animation played out, it still doesn't work half the time.

    Any idea what could be wrong, or a better way I can go about doing something like this? I thought events would be really handy for this.

    EDIT: It seems to be related to frame rate, or animation frame rate, or more likely both since the skipping is not consistent and animation speed is. If I slow down either the game or the animation, it will trigger the event almost every time. Significant improvement. Not a solution though, just an insight. :p
     
    Last edited: Aug 31, 2016
    lucbloom likes this.
  2. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    We did fix a bug in 5.5 lately about low frame rate issue and animation event.

    In any case they should always be fired, so please log a bug with a small project showing the issue and we will fix it.
     
  3. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    Is it ok if I include a free asset in the small project? Since that's what I'm using atm.
    Also if its fixed in 5.5 I don't mind waiting for it (or trying the beta). I'm just prototyping and learning so no rush. :p
     
    lucbloom likes this.
  4. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    yes you can submit a bug report that include a free asset or even a paid asset, the data is not exposed to public.

    Well if you have time I would prefer to have the bug report in the version that you are currently using, it could fix issue for other user as well.
     
  5. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Hey, I'm using 5.6.2f1 and I'm still getting the issue. I ran it once with a break point on the method that should be called and it worked, removed the breakpoint and it didn't call it. Then I put the breakpoint back and it still didn't work. I did have a time scale of 30ish because I didn't want to watch the whole (1 min) animation everytime I was testing but before typing this I set it back to 1 and it still didn't work. It's only worked 2-3 times today and the animation event should have been triggered at least a 100 times. I put the event on another animation and it worked fine! I recorded a video of me doing it the original way and then changing the event on the animation without changing anything else if your interested.
     
    lucbloom likes this.
  6. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    592
    I have the same issue. I'm making a beat em up game, so I need really fast trigger detection. Currently I have the Hitbox Trigger Collider become active at a certain frame of animation. But it's not consistently being called by the animation event :(
     
    lucbloom likes this.
  7. eyalm

    eyalm

    Joined:
    Dec 20, 2015
    Posts:
    42
    No solution? I have this bug now too in 2017.2
    Being not able to trust events make them almost useless.
    Btw I'm using legacy animations, does it makes a difference?
     
  8. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Little note; you need to make sure that animation is currently running for the event to fire.
    By example, if the state is in transition to another state, the transition finish before the end of the animation, and the event is on the last frame, it won't fire.
    The events are called in transition, only if the animation transition from or towards has >0% of blend.
     
  9. JenJon

    JenJon

    Joined:
    Mar 30, 2018
    Posts:
    1
    Thanks for that. I just spent a whole day trying to figure out why my animation event that was set on the first frame of the animation wasn't working! You gave me the solution!
     
  10. RZGames_Jethro

    RZGames_Jethro

    Joined:
    Jul 6, 2017
    Posts:
    87
    still same issue in 2020! :mad:
     
  11. omerselman

    omerselman

    Joined:
    Mar 5, 2016
    Posts:
    20
    yes, they dont. I lowered the animation speed but still the same.

    Found it!
    Believe or not, they actually work. The cause of the problem is not a bug but frame process time and triggered event process time must match or triggered event process time must be lower.
    I tried with 2019 and 2020 versions and it works like charm. To explain what actually happenes is when you call a function from a trigger event, and if the function is loaded with calculations, in my case checking if it hits any colliders, the function process speed can't catch up with frame process speed. I found out that was the reason the trigger event skips the function. It is all about the process speed and fps.
    I changed the function and pre-calculated the colliders before the animation starts and determined the targets before the trigger event fires. When event fires the function, it simply says "okay, i hit this" which is much much less loaded and fires functions consistently all the time.
     
    Last edited: Feb 27, 2021
    ilicstefan0307 likes this.
  12. sergiusz308

    sergiusz308

    Joined:
    Aug 23, 2016
    Posts:
    199
    What worked for me is making sure trigger parameter is reset when it's not used any more.
    Sometimes it gets carried away being triggered, behaves like a bool parameter and this causes various mess in the Animator.