A Question about HIt() Event

I’ve almost completed this course, still at the weapon refactor part. but i’m coming back here to figure out about this Hit() event.

Its quite vague how the Hit() event works. I know its set in the animation clip in the unity editor, but how is this Hit() triggered? The script that sets the trigger for the attack animation? Or is this broadcasted to all the scripts attached to the gameobject?

That’s exactly correct. The Animator sees the event in the animation and broadcasts it to all the components attached to the GameObject. As an added bonus, if no scripts receive the broadcast (i.e. no script has a Hit() method, or whatever event name you use, then the Animator will throw a warning letting you know that there was no script to received the event.

just the gameObject right? how about the children gameobjects? means just the gameobject that contain the animator component.

thanks!

That’s correct. Only the GameObject with the animator on it. The broadcast does not trickle down to children. You can put a listener on the parent GameObject that then sends messages to scripts on child GameObjects, but the animator won’t do that for you.

1 Like

That’s correct. That’s why with animation events you want all your logic code to reside at the top parent level. That parent can always then do other things where invokes method in its children gameObjects or indeed, anywhere else.

So animator, code logic, colliders (where possible), etc… all the things that affect “state” in a logical sense, you really want those things to be put together in the parent gameObject

thanks for the advice!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms