- Home /
Compound colliders with different layers not working as expected (Layer collision matrix)
Hello Unity community,
I have a collision scenario that I thought would make sense, but apparently gives me an issue in Unity. Here is a summary:
I have one of my main game object "ActorGameObject" with a rigidbody. I placed it in layer "ActorLayer". ActorGameObject has a compound box collider (Front and Back), also marked in "ActorLayer". I have different kind of projectile. "ProjectileAGameObject" has a box collider, rigidbody and a damage script. I placed it in "ProjectileALayer". My Layer Collision Matrix has them tagged as colliding. Until now, everything works fine.
But I want to add a protective feature to my actor, a shield. The shield only stops some projectile, but not ProjectileA. So I create "ShieldGameObject" with an encompassing SphereCollider, marked with "ShieldLayer". I nest it inside ActorGameObject because I want to inherit the actor transform (visually and physically) so as to encompass it completely. In the Layer Collision Matrix, ProjectileALayer and ShieldLayer don't collide.
ActorGameObject (Rigibody, script, layer="ActorLayer")
Colliders
Front (BoxCollider, layer="ActorLayer")
Back ((BoxCollider, layer="ActorLayer")
ShieldGameObject (SphereCollider, script, layer="ShieldLayer")
ProjectileAGameObject (Rigidbody, script, BoxCollider, layer="ProjectileALayer")
It doesn't work. My ProjectileA collides with the shield. In the debugger inside ProjectileA.OnCollisionEnter(Collision collision) :
collision.collider is a SphereCollider
collision.gameObject is ActorGameObject
It looks as though the layer value, in case of collision, is inherited from the Rigidbody rather than the collider.
To solve it, I tried:
Suggested in the only other thread I found acknowledging this case, add a Rigidbody marked kinetic inside ShieldGameObject. The logic behind it is to reinherit the layer value from the rigidbody. I tested it and it doesn't work (the layer value still comes from the highest level rigibody).
Place the ShieldGameObject out of the ActorGameObject hierarchy. It works, but I have to synchronize the shield transform to follow the actor, every update. Also (I stopped there, because) I have to synchronize all other behaviour. The actor dies? Kill the shield as well, ... Does that seem like the way to go?
Waiting for an answer, I'm also going to try this next step: move down the actor rigidbody at the Colliders level and synchronize the rigidbody transform with the actor transform. It will limit the number of synchronizing I'll have to do, but I'm afraid of side effects of modyfing the transform of a Rigidbody...
Is there a more obvious solution I don't know about? Thanks for your help.