2
\$\begingroup\$

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.

Edit to add a bit more information as Savlon suggested:

When we are not using compound objects and we want to have a centralized place to handle children collision events, we could have something like this:

//In Children
void OnTriggerEnter(Collider c)
{
    (parent's script).TriggerHandler(this, c);
}

//In Parent
public void TriggerHandler(GameObject theChild, Collider c)
{
   // do stuff
}

This is ok and will work. The "problems" with this way are:

  1. I could have a lot of children, so, using compound objects I would need to add just a rigid body component to the parent. This could be a pretty good memory reduction usage. However if I don't use compound objects I will have to add a rigid body to every child like in the previous code.
  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.

On the other hand when using compund objects, you get all the advantages listed before but if you need information about what children collided with another collider you won't be able to get it because your script listening to callbacks is in the parent and you will only receive as parameter the "other" collider when unity calls you back through OnTriggerXXXX callback.

I was asking if there is any way to get both colliders using a compound object.

Hope everything is clearer now.

Cheers

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Can you try to explain your concern a bit more because it seems a little unclear to me \$\endgroup\$
    – Savlon
    Commented Nov 29, 2014 at 11:03
  • \$\begingroup\$ I have added a bit more of information to the question. \$\endgroup\$
    – Notbad
    Commented Nov 29, 2014 at 12:26

1 Answer 1

1
\$\begingroup\$

You could, at runtime, assemble your children's colliders into one big collider that you add to the parent. In order for this to work the way I've done it below, the children would have to have Mesh Colliders, even if they were box-shaped:

// Use this for initialization
    void Start () {
        MeshCollider[] childMeshes = GetComponentsInChildren<MeshCollider>();

        CombineInstance[] combine = new CombineInstance[childMeshes.Length];

        for (int i = 0; i < childMeshes.Length; i++)
        {
            combine[i].mesh = childMeshes[i].sharedMesh;
            combine[i].transform = childMeshes[i].transform.localToWorldMatrix;
        }

        Mesh mesh = new Mesh();
        mesh.CombineMeshes(combine);

        MeshCollider parentMesh = gameObject.AddComponent<MeshCollider>();

        parentMesh.isTrigger = true;
        parentMesh.sharedMesh = mesh;
    }

After that, it would be up to you to disable the Mesh Colliders in the child components, or not, as your program requires.

See this documentation for more information:

docs.unity3d.com/ScriptReference/MeshCollider-sharedMesh.html docs.unity3d.com/ScriptReference/Mesh.CombineMeshes.html

\$\endgroup\$
0

You must log in to answer this question.

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