- Home /
Raycast2D Layermasks and incomplete docs
Heyho. By the time I'm writing this, I allready solved this problem for me, but i would love to know, why it didn't worked in the first place.
My setup: procedural tilemaps (like terraria), player only in the underground, cinemachine cam following player.
To confine the camera to the underground, without seeing the surface, I added a huge 2d polygon collider to the whole map. The players mining is based on raycasts on the 2d tilemap-collider.
I used different layermasks on both collider variants and wanted to filter the raycasts from the player.
private void PlayerMining(Vector2 direction, Vector2 origin)
{
RaycastHit2D hit = Physics2D.Raycast(origin, direction, 1f, 8)
}
As in the docs is stated, i need to pass in Vector2 origin, Vector2 direction, float distance, [[[int]]] layermask
but this won't work. Debug.Log(hit) stated that it hits the PolygonCollider for the map (which is layermask 9).
I tried Physics2D.IgnoreCollision(8,9), Physics2D.IgnoreCollision(9,8), Physics2D.IgnoreLayerCollision(8,9), Physics2D.IgnoreLayerCollision(9,8). nothing.
I tried to set the collision rules in project settings > Physics2D and Physics (just to be sure). Player and TilemapCollider on Layer 8. cam and polygon collider on Layer 9. nothing.
The only thing that helped me:
public LayerMask obstLayerMask;
private void PlayerMining(Vector2 direction, Vector2 origin)
{
RaycastHit2D hit = Physics2D.Raycast(origin, direction, 1f, obstLayerMask);
}
suprisingly the IDE didnt marked it as false and it finally worked, altough I never saw that i can pass in a LayerMask, instead of an int. Not in the docs, not in the IDE.
I would be very grateful for an explanation. Thanks in advance!
Answer by Crycore93 · Oct 08, 2020 at 07:18 AM
nothing? I'm trying to learn unity and programming and never had a teacher or mentor on this topics, which I could ask. I'm used to google every problem i have and managed to solve everything I needed. It's really hard to accomplish this completely on my own and I would be very grateful for some guidance. Even a short "it's a bug" would be enough.. Unfortunatly my questions are not intresting enough. I dont understand why I never get an answer on my threads :(