Playing new animation still fires events from previous animation

So I just found out about a bug in my game. If I press a button to attack, and then immediately press the button to duck (which will play a different animation), then the ‘DoDamage’ event from the attack animation will still fire.

Why is this, and how can I stop it? I had assumed that a new animation would interrupt the previous one, but apparently not as the events from the previous animation are still firing.
I found a workaround (see below) by enabling / disabling the animator, but I’m really hoping for a better way.

Some more information: I start attack animations in code, and all animations are set to transition to idle afterwards. Oh, and I’m not using mechanim.

I’ve been doing some fiddling around: This will not start an attack animation, and go straight to ducking. No event fired:

animator.Play(attackanimation);
animator.Play(duckanimation);

This will start an attack animation, then immediately do a ducking animation - but the attack events are still fired:

animator.Play(attackanimation);
yieldreturnnewWaitForEndOfFrame();
animator.Play(duckanimation);

This will start the attack animation, then immediately do a ducking animation - no attack events are fired.

animator.Play(attackanimation)
yieldreturnnewWaitForEndOfFrame();
animator.enabled =false;
yieldreturnnewWaitForEndOfFrame();
animator.enabled =true;
animator.Play(duckanimation);

OK so I’ve implemented the coroutine that disables and then enables the animator, and it works… still, I’m not happy with that solution. Anyone know a better way to prevent the events from the previous (interrupted) animation from firing?
Thanks :slight_smile:

If you are using an Animator component you are using mecanim.

This is not the expected result, as soon as an animation clip stop been played it should not fire any animation events.
Could you log a bug with your project please?

Thank you very much for your reply! I’ve created a simpeler project with only 1 scene and 3 buttons that will demonstrate the problem, and submitted a bug report.
Thanks again :slight_smile:

1 Like