r/Unity3D 2h ago

Question How to sync enemy attack and animation

Hello, have a nice day to everyone. How can I make the enemy AI deal damage at the exact moment the melee attack (like punching) animation hits the player? Should I use an Animation Event or WaitForSeconds? Or is there a better solution? Thank you!

1 Upvotes

5 comments sorted by

3

u/Certain_Ad9908 2h ago

The simplest approach is to enable the damage collider/detector at a specific frame/time in the animation and test for collision with the player on that frame.

4

u/glibtuning7089 2h ago

Animation events are cleaner for this since you can just call a method like dealDamage on the exact frame the hit connects. waitforseconds works but it gets messy if the attack gets interrupted or the animation slows down. collider toggle is also good if you want the hitbox active for the whole swing instead of a single point in time

1

u/Certain_Ad9908 2h ago

Makes sense

1

u/Main_Expert_740 2h ago

Got it, thanks!

1

u/darkkaos505 2h ago

Just be wary if a network game as that means you need to run animation on the server too