Using specific Collider2D if more than one are attached to a single gameobject

Hi,

So I have a tile of a street as a gameobject. Attached to this gameobject are two Colliders on both the right and left side. If the player touches these Colliders he will die. Now I want to put another collider on the same gameobject but this time the player should not die when touching it.

I cant call the OnCollisionEnter2D funktion because it checks all colliders and doesnt differentiate between colliders…

Is there any function or way to call something like the OnCollisionEnter2D function for a specific Collider2D?

I am sorry if this is answered in a different thread already but I didnt find anything.

Thanks in advance

Think the only way to accomplish that is by actually moving the collider to a different gameobject, parented to the original one, and change its layer, and do likewise to the player, UNLESS you just want simple collision, in which case you keep the killing ones as triggers but the other one as a non-trigger collider.

That said, not sure how worth it that is. You can still look at the tag of the collider when doing your collision check function and just filter based on that yourself. I got a feeling that under the hood unity already doing pretty much this for different layers so you’re probably gain very little by avoiding doing the filtering yourself.

1 Like