- Home /
How to detect when an animator loops?
Hi,
I have a 2D sprite monster that follows the player and attacks when in melee range, I'm using an animator controller (state machine) to switch between the run animation and the attack animation.
The switch between each animation works fine, and the attack animation starts when the monster is next to the player, the problem is that I need to do some stuff each time the player gets hit, that is in the last frame of the monster's attack animation.
Is there any way to detect when the animation ends and starts again?
I tried adding a script to the attack state (its a blend tree), but I couldn't find any property or method to check if the animation is playing.
I also tried not looping it, but the animator doesn't seem to dispatch an event when the animation ends either so I don't know when to play it again.
I don't want to use a timer, the idea is that this should work without knowing exactly how long the animation is so I can use it with different attacks.
Thanks!
Answer by MartinTV · Dec 30, 2015 at 05:48 AM
Sounds like you would want to use Animation Events
Here's a video explaining it: https://www.youtube.com/watch?v=QNdGBBsC9_I
In short, you can trigger a method in code at a certain frame in an animation.
Hope that helps!
~Martin
Edit: Jumping to 5 minutes is probably all you need
Thank you $$anonymous$$artin! This exactly what I was looking for.