- Home /
The question is answered, right answer was accepted
Why is Raycast hitting masked layer?
Many questions have been posted on this topic but I'm facing a weird behavior which I could not find an explaination for.
I perform a raycast giving it a LayerMask parameter which is defined as public variable and set in the inspector with thick on layer "Wall" and "Enemy".
At first glance it appeared to work fine because raycast rightly stops on GameObjects with that layer.
But then I saw that it stopped also on GameObjects with default layer, like if the LayerMask was ignored. Is there something I'm missing?
According to this answer I set up everything correctly and I expected it to work fine.
Looking at the inspector at Runtime I can clearly see that my mask is set only to "Wall" and "Enemy", and that the GameObject the collider is attached to has default layer set. I'm then stating that the mask the way I'm using it does not exclude other layers. The only way I found to have this working is by setting that GameObject layer to Ignore Raycast but in this case the LayerMask is not necessary.
I'm calling the function this way:
RaycastHit2D inSight = Physics2D.Raycast(start, end, lm);
Maybe is there something different using Physics2D?
I would really enjoy touse LayerMasks instead of giving specifically Ignore Raycast to whatever I want to ignore. Any clue?
@meat5000 may you reopen the question? I accepted an answer that was not correct
Answer by MadDevil · Oct 13, 2015 at 06:05 AM
giving a layer, while raycasting means that that raycast will return any info if it hits the objects of that layer. but i can collide with every layer.
if you want that you can use ignoreraycast layer.
So, you are saying that to have a raycast pass throughout a collider then only thing i can do is use RaycastAll() or use ignore raycast layer? And then, Layer$$anonymous$$asks does not serve for this purpose? This would be WOW!
Invert your layermask to avoid a layer. It will hit all other layers.
Layermask is used when you want only some objects to return the hitinfo not all in the scene. you can sort out objects with help of layermask.
@$$anonymous$$adDevil So if my need is to reach a certain layer passing THGOUGHOUT other layers I'm right in saying that I need to use raycastAll or set layers to be ignored to "ignore raycast"?
Follow this Question
Related Questions
Why is RaycastHit2d Not working 0 Answers
2D Raycast Layer Mask not working 1 Answer
Bitmask/LayerMask used in raycast not working 2 Answers
Raycast and Layer Masks 1 Answer
Physics 2d raycaster from EventSystems for overlapped colliders 0 Answers