- Home /
Getting OnCollisionEnter to fire on composite object
Question
Given the following hierarchy of objects/components:
Parent Object : rigidbody |_ Child Object 1 : mesh filter, mesh renderer, mesh collider |_ Child Object 2 : mesh filter, mesh renderer, mesh collider
I want to detect when one of the child objects is hit. I've created a basic collision detection script using the OnCollisionEnter callback however the collision event callback is not fired.
public void OnCollisionEnter(Collision collision)
{
Debug.Log("Collision detected on " + gameObject.name);
}
Investigation **
If I take the child object and add a rigidbody without changing anything else, this works fine:
Parent Object : rigidbody |_ Child Object 1 : mesh filter, mesh renderer, mesh collider, rigidbody |_ Child Object 2 : mesh filter, mesh renderer, mesh collider, rigidbody
However this gives the incorrect behaviour (it's no longer a composite rigidbody) and I want to avoid having to join all the child objects together with constraints.
In the desired hierarchy, detecting collision on the projectile works fine. I'm guessing the problem is something to do with the mesh collider and rigidbody being on different objects.
The documentation states:
A Collider does not necessarily need a rigidbody attached, but a rigidbody must be attached in order for the object to move as a result of collisions.
And the object does indeed move after it has been hit correctly, I just don't get the collision event.
Any help greatly appreciated!
You're right, I'd expect that to work especially if the object is moving correctly ($$anonymous$$eshColliders can be a nightmare) - I'm guessing that your projectile is a primitive?
Oh hang on - you are checking for the collision on the parent right? You only get the collision on the thing that has the rigidbody. (Thinking you could possibly add a kinematic rigidbody to the children or you are going to have to do some more work...).
If I recall correctly, when using $$anonymous$$esh Colliders you must also include a rigidbody... or a least a rigidbody on one of the two items colliding. I could be wrong about that but seem to remember reading it.
Thanks for your responses!
whydoidoit: I was checking for the collision on the child object. I guess looking at the Collision object I could go collision.collider.gameobject to get the child. I'll try adding the script to the parent and see what I get.
cassius: the composite object has a rigidbody on the parent gameobject, the projectile is a single gameobject with collider and rigidbody (the event does fire on this one).
You may want to consider using a more primitive collider and seeing if that simplifies things at all. $$anonymous$$esh colliders are pretty heavy performance-wise, and there is added complexity as a result.
Answer by Ranom · Nov 21, 2012 at 11:18 AM
whydoidoit: you're spot on:
Move the collision script to the parent object.
collision.contacts[0].thisCollider.gameObject returns the child gameObject in the hierarchy.
Answer by sparkzbarca · Nov 15, 2012 at 06:34 AM
judging by the info on compound colliders here.
http://docs.unity3d.com/Documentation/Components/class-Rigidbody.html
compound colliders cant be made up of multiple mesh colliders.
only a collection of primitive colliders.
Mark as answered please. :)
That is wrong - you can use multiple meshes as compound colliders.
If you read the question again - you'll see he has the right physical effect already (meaning his colliders are already working in PhysX)- as I say in the comments above, the problem is checking for the collision on the child when it will in fact be reported on the parent.