Associate a Collider on a Rigidbody that's sitting on the parent for collisions detection

Hello all !! :slight_smile:
Thank you for taking the time to read my question.

What I would like to do is to have a GameObject that can interact with other elements of the scene when moving. The thing is that I would like to separate the logic from the shape of the object. What I did is to create the following hierarchy:

Parent GameObject with Rigidbody and collision handling logic
| → child GameObject with more logic not related to collisions
| → child GameObject with the mesh and the Collider

what I did is to add to the child with the mesh and the Collider a CollisionMessagesForwarder script that other scripts can register to. Basically, the forwarder listen for OnCollisionEnter, etc… and it will fire the proper event when something happens to the Collider.
Awesome !! this doesn’t work: the OnCollisionEnter, etc… never happens on the Collider because the Rigidbody is on the parent of the mesh, therefore, Unity doesn’t get that a collision with something else happened.

Obviously I could move the Collider on the parent of the mesh, BUT seen that the mesh scale can change at runtime thanks to a script that interacts with its scale, I don’t really want to change also the Collider, therefore, it’s easier to keep the collider on the mesh so that it adjusts itself automatically.

I could also add the Rigidbody to the child with the mesh and the Collider, BUT the Rigidbody is also used to move the entire GameObject, therefore, it should sit on the root so to not move just the mesh around also because in the root there is a Destroyer component that checks in different ways if this object is still valid (time to live, Y axis limit, camera frustum, etc…)

Obviously, I really want to keep the logic separated from the mesh so that I can create several prefabs with different shapes without having the need to modify the common logic on the object.

I also tried to add a second Rigidbody just for collision detection purposes on the mesh, but the result is that the object goes wherever it feels to go !! :smile:

So… the question was if it is possible to tell the Collider on the child to use the Rigidbody on the root to detect the collisions (something like collider.attachedRigidbody = root.rigidbody)… or do you have a better solution for the mess I did create? :slight_smile:

Oh… I forgot to mention: the physics interactions have been customized setting layers on the GameObject so to minimize the impact on the physics system by ignoring collisions between objects that are not relevant each other. This means I also changed the physics matrix in the settings… I don’t know if this changes the way Colliders work with Rigidbodies…

Thank you very much for your replies. :wink:

2 ideas:

  1. You could go from OnCollision to OnTrigger and make the sub-collider a trigger. But that might mean you’d need to do a lot of the physics by hand.

  2. Use the sub-collider to keep track of what the main-collider should look like and if main-collider and sub-collider don’t match, you replace the main-collider with the sub-collider. This would allow your to use any shape and size as a sub-collider and mesh, and it’s just fed into the parent to complete the gameobject. This, of course, can lead to performance issues, if you need to do too much of it.

Otherwise, there might be some Unity magic I do not know yet.

Thanks for your suggestions !! :slight_smile:

Actually, before to read your reply, I was trying to do something similar to your second point. to be honest is kind of working: I’m giving the duty of colliding with physical to the inner Collider whilst I’m setting up a Trigger on the root of the object by applying at runtime the Collider’s bounding box to the Trigger.
Obviously I’m kind of worried about the performance hit: as you mentioned I’m doubling the physical calculations on an object that can be cloned multiple times in the same context.
I will test the entire thing and if the performances are good enough then… woohoo !!! :stuck_out_tongue:

Anyway, any new Unity’s voodoo suggestion is always welcome !! :wink:

You’re kind of doing double the work. What I mean is, the parent does all the heavy lifting, the child only checks if the parent has the correct collidercomponent, and if the parent for some reason has changed its collider in some way, you apply the original collider again.