0
\$\begingroup\$

I want to detect when a Child's collider is being triggered to apply the damage to the thing being hit. But currently I'm confused about how it works.

Does the Parent's OnTriggerEnter detect the trigger events of the Child Object ? And how do I know the trigger come from the Child and not the Parent, and know which Child's collider got triggered?

The Parent's collider is not a trigger, but the Child's collider is.

Enemy sprite attacking, with collider outlining the hit area of the attack

\$\endgroup\$
3

1 Answer 1

1
\$\begingroup\$

Triggers don't create OnTriggerXXX messages, only rigidbodies do. If child doesn't have a rigidbody, basically its collider is considered a child collider of the parent node (compound collider). Now we come back to the question:

Does the Parent's OntriggerEnter detect the trigger event of the Child Object ?

If child object doesn't have a rigidbody, yes.

And how do I know the trigger come from the Child and not the Parent and know which Child's collider got triggered ?

They need their own rigidbody and script with OntriggerEnter, which is not recommended.

In fact, you don't need to get the physical events from the attack box, simply set it to another layer, such as "attack", and all attack events are handled by the attacked party (character's body, with rigidbody and script, layer "be_attacked").

\$\endgroup\$
3
  • \$\begingroup\$ Oh so I can check the GameObject layer that being hit in the OnTrigger and see if it's the layer I want to get hit by the Child's Collider ? \$\endgroup\$
    – Thien
    May 31 at 9:14
  • \$\begingroup\$ You can set the Collision Matrix in Edit > Project Settings. So that collision will only happen between the objects you want. \$\endgroup\$
    – Mangata
    May 31 at 9:24
  • \$\begingroup\$ Thanks @Mangata that's really helpful I got it now \$\endgroup\$
    – Thien
    May 31 at 9:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .