How to Use Layer Mask

Hi, I try something pretty normal which is to make my raycast ignore some layer. I use a public Layer mask and dit this

        if (Physics.Raycast (RayOrigin, m_Camera.transform.forward, out GunRaycast,mylayerMask))

Now no matter what I insert in my Layermask in the editor, it change nothing at what the raycast do, he always return everything he get. Any Idea what I do wrong?

Take a look at the documentation for Physics.Raycast:

Edited:
example wasn’t correct, see below

I think you may be right in part, sadly the solution itself give me an error

`UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3, float, int)' has some invalid arguments

I haven’t used this much, but I think you have to use mask.value to convert it into an int.

I believe so :slight_smile:

Thanks for the help but no thats not the problem cause if I use this structure

        if (Physics.Raycast (RayOrigin, m_Camera.transform.forward,100,mylayerMask))

I dont need the mask value to work it out, the public layer mask work just fine but I dont know how to get the value of the object I touch since it refered to no variable, and I tried adding myraycast hit in many way and it dont work out.

On the other hand if I use the first structure,

Physics.Raycast (RayOrigin, m_Camera.transform.forward, out GunRaycast

I can insert a layermask without getting error but the layer mask does not seem to work out.

And what annoy me the most is that I can do that in 2D

test=Physics2D.Linecast(RayOrigin, m_Camera.transform.forward,100,mylayerMask)

and I know for a fact that it work ,but in 3d it say that I cannot convert it to bool.

        GunRaycast= Physics.Raycast(RayOrigin, m_Camera.transform.forward,100,mylayerMask);

Again thanks for your help but I still cant figure it the missing piece of the puzzle.

2d and 3d may be different. This is what I did in my game:

                int layerMask = 1 << 8;
            if (Physics.Raycast(ray, out hit, 15, layerMask)) {blah}

I think it’s called bit shifting, it was the old way, but you can see it needs an int. It works by the way. It ignores things that aren’t on the layer mask in 3d.
Point is you need an int value for layermask. Question is, is your layermask an int? If it isn’t, then you need to use .value. for your layermask. If you looked at the link I posted, it shows using .value in the example below the definition.
The error you got said that you weren’t giving it the right type, that would follow.

no my layermask is a layermask lol but thanks I will definitly try

It’s just a matter of matching the parameters to the signature you want to use. If my previous example didn’t work, and it’s showing “invalid arguments” it means the parameters aren’t matching a defined signature.

I gave that example without compiling or anything, so it’s probably that those parameters aren’t optional as they appear in the docs, or rather, the docs are simply showing default values.

The conversion from LayerMask to int is implicit, just pass it in.

Try this:

Ray ray = new Ray(origin, direction);
RaycastHit hit;
LayerMask layerMask;
float maxDistance = Mathf.Infinity;

Physics.Raycast(ray, out hit, maxDistance, layerMask);

Configure those values as you wish, in the inspector, in code, etc.

This is using the highlighted signature: