Receiving OnCollisionEnter from child GameObject

Hi all,

I’ve got an issue where I want to create dynamic bounds for a brick breaker level at runtime using CreatePrimitive and then be able to listen for OnCollisionEnter with the parent. I’m trying to avoid having to create another script just for this basic collision detection.

I figure I could just use a Collider, but I want to be able to see the blocks.

Here’s basically what I’m doing.

void Start()
    {
        //draw the left wall
        GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube.transform.parent = transform;
        cube.name = "left";
        cube.transform.position = new Vector3(wallThickness / 2, bounds.height / 2, 0);
        cube.transform.localScale = new Vector3(wallThickness, bounds.height, wallThickness);

...

}

void OnCollisionEnter(Collision other)
{
  //here's where I want to deal with the collision of cube
}

Thanks for any help in advance.

Cheers

just create the colliders on all childs and put a rigidbody on the parent that is setup correspondingly.
all collision callbacks then will be sent to the parent with the rigidbody component

1 Like

hint;
collision = the main gameobject
collision.collider = childs

1 Like

thanks dreamora!! That worked perfectly.

Just to follow this up. Can I have a rigidbody that has static colliders and static trigger colliders? I would like my object to be able to handle both OnTriggerEnter and OnCollisionEnter if possible.

Thanks again!

It is possible to have two colliders with one set as a trigger and the other set for normal collisions. If the colliders are of different types (say, box and sphere), you can attach them to the same main object. Otherwise, you will need to create child objects for the colliders with a rigidbody on the parent.

I have strange problem …

I create the colliders on all childs ( each child has a rigidbody ) , and put a rigidbody on the parent.

But all child won’t collision callbasck to the parent OnCollisionEnter…