r/UnityHelp • • Aug 22 '22

Raycast in Unity2D

When using a Raycast with Physics2D how do I make it detect only one layer.

[Serialize Field] private LayerMask floor; 
RaycastHit2D hit;
hit = Physics2D.Raycast(transform.position, other.transform.position, floor);

The above is the same code except the point I am using as my Raycasts target is a specific GameObject. The Raycast works, but it always detects the player. I want it to detect only the layer set in the floor variable.

3 Upvotes

2 comments sorted by

View all comments

1

u/_vert Aug 22 '22

I can't remember how but you use a layer mask