Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Get the own child that collided with another object in a compound object?

Discussion in 'Scripting' started by hexdump, Nov 29, 2014.

  1. hexdump

    hexdump

    Joined:
    Dec 29, 2008
    Posts:
    443
    Hi,

    I have a compound object. The parent of the compound object hierarchy has a script that is listening to the OnTrigerXXX callbacks of the children. When this compound object collides with other objects is there any way to get both colliders? I mean the "other" collider and the collider that belongs to the compound object itself that collided?

    P.D: All rigid bodies must be kinematic, so, I can't use OnCollisionXXXX with the collision info param that has all the information I need in the contacts array.

    Cheers
     
    Last edited: Nov 29, 2014
  2. fox4snce

    fox4snce

    Joined:
    Jan 25, 2014
    Posts:
    74
    A little confusing sounding...

    Why can't you do this...

    Code (CSharp):
    1. void OnTriggerEnter(Collider c)
    2. {
    3.     (parent's script).TriggerHandler(this, c);
    4. }
    5.  
    6. // in parent script
    7. public void TriggerHandler(GameObject theChild, Collider c)
    8. {
    9.    // do stuff
    10. }
    11.  
    There is no reason you can't add the child and the Collider c to a list in the parent script and then process the entire list instead of just one at a time.
     
  3. hexdump

    hexdump

    Joined:
    Dec 29, 2008
    Posts:
    443
    Hi,

    This is exactly what I'm doing know. I'm delegating the call from the children to the parent. This is perfectly feasible.

    The only concern is that for my case it seemed compound objects where the way to go because:

    1) I could have a lot of children, so, having just a rigid body component in the parent could be a pretty good memory reduction. In the case you expose, I need a rigidbody per child.

    2) In my case children under a parent are always touching. Not intersecing but touching. Think about a set of blocks one beside the other. They do not intersect but there are no gaps between them. With compound objects I don't have to mess with it. It authomatically treats all the children as a whole and no collision callbacks will be fired among them.

    More or less these are the concerns I see and why I want to use the compound object way.

    Cheers.
     
    Last edited: Nov 29, 2014