I have a gameObject which have several child and each child have it own collider.
How i can know which child collider is touched by other gameObject?
I have get an bad infor from searching in google, all the child collider will belong to parent, it will only return parent name.
Is there have anyway to get the child name when the child collider is touched?
thanks you in advice.
Using OnCollisionEnter should get you the name of the collider currently being hit. For example, attaching the following to each of the children you want to monitor:
print(gameObject + " has been hit");
should print the name of the child it’s attached to to the console.
I was searching for ages for a solution and this is it. For anyone interested, here is a sample message for OnCollisionEnter which helps explain the process.
void OnCollisionEnter(Collision collision)
//My character is a parent player with two children, each of the children
//has a collider on. One child is tagged "Player_Head", the other is
//tagged "Player_Jacket" (you could also use cp.thisCollider.gameObject.name).
//As far as I know, collision.contacts is the only place where "thisCollider"
//can be found.
ContactPoint cp = collision.contacts;
if (cp.thisCollider.gameObject.tag == "Player_Jacket")
[Solution derived from @SpaceManDan’s observation in a comment here: How do you detect which child collider was hit? - Questions & Answers - Unity Discussions ]