Detect collision with character controllers on the same layer, but allow objects to pass through one another

Hi, I’m having a bit of trouble. I’m working in 2D, and I have two players with character controllers stored in a “players” layer, and a ball in the middle of the screen, also with a character controller, stored in a “ball” layer. The players are displayed by having their sorting order changed depending on their y-position, so the player closest to the bottom is displayed over the other.

I’m able to detect collisions between these objects, but how would I be able to allow movement through each other while still detecting collision and keeping the objects in the same layer?

What I can think of is little detour to get what you want.

  1. Both players are on Layer: Player and collision on Player-Player is disabled.
  2. Both players have child obj that has second identical collider as the main collider, but this child object is on layer PlayerTrigger (or what ever you want to call it) and the collider has “Is Trigger” set as true.
  3. Now players can pass through each other as their colliders on layer Player won’t collide, but you get OnTriggerEnter / OnTriggerEnter2D when they enter each others colliders.

If you want to detect only collision triggering try to set ‘isTrigger’ for your colliders and use OnTriggerEnter method Unity - Scripting API: MonoBehaviour.OnTriggerEnter2D(Collider2D) instead of OnCollisionEnter.