- Home /
Usage of Layer masks, what am I doing wrong?
I'm looking into using layer masks to make my raycasts ignore certain layers. I've named Layer 8 (the first one open to editing) "Ground" and set my ground object to this layer, and then tried to use the following code (the idea is to only make the ray hit register if it hits the ground object):
Ray ray = Camera.main.ScreenPointToRay(origin);
RaycastHit hit;
//set layermask to test for "Ground" layer (layer 8)
int groundLayer = 8;
int layerMask = 1 << groundLayer;
if (Physics.Raycast(ray, out hit, layerMask))
return hit.point;
It operates just like a normal raycast and hits everything not in the "ignore raycast" layer. What am I doing wrong?
I tried looking into it and found links like these:
http://answers.unity3d.com/questions/416906/make-raycast-ignore-some-objects.html
http://answers.unity3d.com/questions/8715/how-do-i-use-layermasks.html
But I don't really get what I'm doing wrong.
It should work normally, can you please try putting $$anonymous$$athf.Infinity before layer$$anonymous$$ask? It might recognize it as distance. another way of defining layermask would be; public Layer$$anonymous$$ask rayLayer$$anonymous$$ask; then you can choose which layers to get affected by the ray.
Answer by whydoidoit · Apr 03, 2014 at 07:58 PM
You are passing the layerMask in the distance parameter not the layer mask one.
Can you explain a bit more? I mean, look at this screenshot:
Looks like a layer mask parameter to me :S
Edit: Oh, never$$anonymous$$d, I think I see. That one lists float distance as the second parameter which is the wrong one. Weird that it did that it'd show that since I already filled in a raycasthit. I'll look into it a bit more.
Your picture shows a version of Raycast that takes a distance, you are using the one that has an out Hitinfo - that requires distance and layer mask.
Yeah I see that now. The problem with this is that I'm using this to find a location my mouse is hovering over, so how could I enter the distance as an input before I know where the point is?
$$anonymous$$ake it float.PositiveInfinity - that's what happens if you don't pass one.
Oh, the distance parameter is the distance you're checking for a hit in, and not the distance between the origin and the point?
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Help with Layermasks 1 Answer
RayCast vision testing for same layer objects? 2 Answers
How do I use layermasks? 9 Answers
Layer mask doesn't work... 1 Answer