Layers for 2D Colliders?

I am currently working on a new scene for a 2D point and click adventure and I have a little issue. In the scene I have a collider covering the whole Scene to allow the player clicking everywhere and letting the character move to the closest spot (using a nav mesh an AI path finder and a seeker script for tracking the MousePosition). Now I added a second collider for an Item wich obviously is at the same place as the ground collider. Now my Question: Can I somehow add Layers or something to the colliders to decide in which row they are so the RaycastHit hits the Item collider at first, becouse it doesnt do anything but hitting the ground collider?
Please give an answer a real noob can understand XD

ps: If you need any more information pls tell me but I think my issue is understandable somehow.

Yup. It works the exact same way as with 3D colliders (if you’re familiar with that).

In Unity, navigate to:
Edit > Project Settings > Physics 2D

There, you will find the Layer Collision Matrix, which you can use to define which layers can and cannot collide with which other layers.

Once you’ve set that up, add all you need to do is add a LayerMask argument to your raycast calls to determine what the ray can hit.

1 Like

Thank you, you really helped me out

Know that in 2D a raycast “into” the screen doesn’t make any sense because there is no Z at all so it’s just a point and a degenerate ray. In 2D it’s Physics2D.OverlapPoint.

im very new and ik this is a necro but if anyone knows how to add a mask advice would be appreciated