Child gameObject with collider not colliding if parent has rigidBody
I have a "Player" gameobject with a rigidBody with isKinematic=false (for enemies' weapons to detect collisions when hitting it) and a CapsuleCollider. The player has a child gameObject "weapon" that has a BoxCollider and a script attached to it that detects collisions with the OnCollisionEnter(), when hitting an "enemy" rigidBody.
I have an "Enemy" gameObject with a rigidBody (isKinematic=false) and a CapsuleCollider attached.
Whenever the player's weapon hits the enemy, the OnCollisionEnter() of the player's weapon is NOT called. Only when I remove the player's parent rigidbody, the OnCollisionEnter() of the child weapon suddenly starts to be called.
Is the "player" rigidBody interfering with its child weapon collisions? Why??
Answer by hexagonius · Feb 21, 2017 at 04:36 PM
this behavior is by design. a rigidbody combines all it's child colliders to a form a compound collider.
adding another rigidbody as kinematic to your weapon breaks this up again.
Answer by kkrac · Feb 21, 2017 at 05:07 PM
Thanks @hexagonius, I'll try your suggestion about adding another rigidbody as kinematic to the weapon.
Now, I still don't understand the reasoning behind the parent's rigidBody affecting the child's collisions. Why does it do that? What does it have to do? If the weapon has a collider, and the object being hit (in this case the enemy) has a collider and a rigidbody, why isn't the collision being detected?
UPDATE: adding rigidbody as kinematic didn't work. What did work was adding a rigidBody (non-kinematic and freezing all rotations and axes) on both weapons (player's and enemy's). I recall that solution causing issues in the past... but now, so far, it has worked as expected.
this manual page explains it. go to collision where the weapon is shown:
https://docs.unity3d.com/$$anonymous$$anual/class-Rigidbody.html