- Home /
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?
Answer by Kossuranta · Feb 22, 2018 at 02:03 PM
What I can think of is little detour to get what you want.
Both players are on Layer: Player and collision on Player-Player is disabled.
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.
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.
Answer by dmitriy-untilov · Feb 22, 2018 at 01:58 PM
If you want to detect only collision triggering try to set 'isTrigger' for your colliders and use OnTriggerEnter method https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html instead of OnCollisionEnter.
I was thinking that, but I don't have the option when using a character controller as the collider is part of the controller, and it's not recommended to set it in script.
Your answer
Follow this Question
Related Questions
Tile based movement collisions 0 Answers
Top Down 2D: How to make a game perimeter that keeps objects inside 1 Answer
How to set a layer with code using collisions? 1 Answer
How can I make 2D movement less jerky on a controller, with velocity and such? 0 Answers
wasd movement rigidbody no bouncing 0 Answers