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
}
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
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.
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.