- Home /
Possible to use child OnCollisionEnter with parent Rigidbody object?
I was wondering if it was possible to use the OnCollisionEnter function on a child of a Rigidbody object, as the compound collider will only get activated once a hit. Using another kinematic rigidbody would not work as I would here need to implement manual collisions to stop objects from continue falling.
Or if that isn't possible, are there any way to detect multiple OnCollisionEnter calls(one for each of the child objects hit). Using collision contact points will only get me the first hit.
I have been looking around, but haven't found any answers that corresponds closely enough to my question to help me. :( Any help would be greatly appreciated. :)
Answer by DamienSturdy · Mar 06, 2012 at 07:56 PM
Could you use SendMessage() attached to a script on each child, which calls the OnCollisionEnter of the parent object?
Hm, not sure how that would help, as it is the parents OnCollisionEnter that is called even if an object collides with a child object. $$anonymous$$y problem would be that I want the childrens own OnCollisionEnter to be called, not the parent one. Or have the parent somehow notice collisions on multiple impact points not necessarily just on the first OnCollisionEnter. :)
If you take a look at the example OnCollisionEnter documents, it itterates through each contact point. Each contact point also provides access to both "this" GameObject and the colliding GameObject. You can then, if you must, use GetComponent and call a function in a script of any of the collided objects.
http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionEnter.html
That is true, but it would still only be called one time since a rigidbody makes all the child colliders part of its own. I have been trying to use the contact points in the OnCollisionStay, but I can't get it to work stable enough.
What I'm trying to do is to make objects, like a chair, play a collision sound. As a chair can hit the floor multiple times without leaving the first OnCollisionEnter call the compound collider locking the rigidbody does makes it somewhat challenging.
What you want to do, perhaps, is simply count the number of collisions in OnCollisionStay() and if this count increases, play a sound. You would also play the sound on OnCollisionEnter() (if OnCollisionStay is only called AFTER the initial collision otherwise you'll play the sound twice.) - Should be pretty easy to just use the collision count to do that.
You would probably use velocity to deter$$anonymous$$e the volume of the sound. (Not sure what the best way to do that is off the top of my head- Normally you'd detect a new collision in the collision list and use its normals.)
I think collision point counting might be the solution. I was thinking way to complicated on the OnCollisionStay function. :P The biggest challenge now will be to play the sounds at the right place, but that might not be needed, not sure if people would notice.
Thanks for the help, really appreciated. :D
Your answer
