Raycast ignores my layer mask?

Hi, this is my first question in Unity Answers.

I want to do a ray cast against layer 9 so I wrote this piece of code

				Ray r = camera.ScreenPointToRay(touchPosition);
				RaycastHit hit;
				int mask = (1 << 9);
				if(Physics.Raycast(r, out hit, Mathf.Infinity, mask))
					startPosition =touchPosition;
					catchedObject = hit.transform;
					Debug.Log("Touched object " + hit.transform.gameObject.name + " layer is " + hit.transform.gameObject.layer);

Surprisingly, the output is

Touched object MainCamera layer is 14

The Main Camera has a sphere collider with layer 14, but the ray cast mask should filter it, right?

So, where is the problem?

Make sure you’re not passing in the layer mask in the distance argument position.

Do This

    if (Physics.Raycast(transform.position, transform.forward, out hit, Mathf.Infinity, SelectionLOSCheckMask))


    if (Physics.Raycast(transform.position, transform.forward, out hit, SelectionLOSCheckMask))

mask is an int. It should be defined as type LayerMask.

Also it’s easier to do :-

public LayerMask mask;

Then set the desired layer(s) in the inspector. Then you don’t need to do the binary stuff.

Make sure you’re using LayerMask.GetMask

LayerMask.NameToLayer returns the index of the layer as seen in the layer inspector, not the mask.
Pretty easy mistake to make, since it’s the LayerMask class, so I hope Unity would update their comment for the NameToLayer method to make this clearer.

From the docs:

layerMask:    A Layer mask that is used to selectively ignore colliders when casting a ray.

So you have created the mask that ignores only layer 9


int mask = ~(1 << 9);

For the exact opposite meaning

After a few tests, I think there’s a bug for the Physics.Raycast api (the layerMask behaves abnormally).

I tested the one @Lone-Coder mentioned

Physics.Raycast(transform.position, transform.forward, out hit, Mathf.Infinity, SelectionLOSCheckMask

and the one the @chema.sre used in the question

public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance, int layerMask)

Both failed for the correct layerMask. and if I do it like @foxor , it works… But the result is not what I want since other objects of different layers will also return true if a ray intersection happens.

If I change to Collider.Raycast, there’s no such problem.

Plus, I’m using Unity 2019.3.12f1

This is why you should always read the params info that pops up with intellisense.

try typing LayerMask… disaster restoration Then set the desired layer(s) in the inspector

Had similar problem, does your target object is child of a game object that have a Rigidbody and a collider?
try adding a kinematic rb. to your target.