Linking compound colliders at runtime

I’ve got a prefab which contains a Rigidbody (and some common scripts), but no colliders. After this prefab is instantiated, another prefab, which contains the visual model and a collider, is instantiated and made a child of the object that contains the rigidbody.

However, the rigidbody at this point doesn’t seem to be made aware of the new collider. Is there something else I can do to cause the rigidbody to re-examine the hierarchy to look for colliders?

I know that unity supports colliders as children of rigidbodies, but it appears that those relationships need to be setup in advance.

Any insight would be greatly appreciated.

This is kind of a hack, but I’ve found that manually resetting the rigidbody’s center of mass causes it to recalculate its compound collider.

But if there’s a another, better way of doing it, I’d like to hear as well!

I did what you suggested, the end result was a little weird. First of all, I had to add a ‘junk’ collider between the hierarchy. Because the object was constructed like this.

Prefab_A (Contains rigidbody)
  HullMountPoint

Prefab_B (Contains collider)

After instantiating Prefab_A and _B, I parent HullMountPoint to _B giving me…

Prefab_A
  HullMountPoint
    Prefab_B

I had to add a collider to HullMountPoint in order for it to detect the chain. However, at this point the ship starts colliding with itself (Also, resetting the center of mass didn’t seem to do anything, I had to set ‘detectCollisions=true’).

After using Physics.IgnoreCollision to disable collisions between HullMountPoint and Prefab_B, I discovered that only HullMountPoint collides with anything in the game environment. Prefab_B’s collider is still ignored by everything except HullMountPoint (which considers it to be a separate rigidbody).

I may have to simply attach a collider to the Rigidbody and set its parameters based on values provided by Prefab_B.

Hopefully there’s an explanation for this behaviour, and a workaround.

Thanks for your help.

I have the same issue. However the only reliable way I have found to reset the RigidBody is to destroy and recreate it.

Unfortunately since Destroy is not immediate, I have to wait until the next frame to recreate it. Meanwhile my scripts that expect a rigid body throw exceptions. I guess I could add checks to each one.

I can destroy and recreate instantaneously if I use “DestroyImmediate”, however use of this function causes much wailing and knashing of teeth by the unity developers.

I want to change exchange a character’s shape for another. I guess what I could do is create a whole new player heirachy and swap at some point, but I have been following the character generator example that makes mesh changes in place and it would be a pity to have to use an entirely different mechanism just because I am also changing the collider.