Physics.Raycast ignores LayerMask

Yes, yes, I know there’ve been multiple questions about the exact same title and topic.

However none of them helped. It’s like my Physcis.Raycast behaves in other way. To test my problem I created simple Unity project. There’re only 3 objects in it: a Sphere, a Box & a Cylinder. I also added 3 user layers of the same names. As you may suspect, Sphere is on layer Sphere, Box on layer Box, and Cylinder on layer Cylinder.

Then I created this script:

using UnityEngine;

public class HitMe : MonoBehaviour
    public LayerMask layerMask;

    void Update()
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        bool gotHit = Physics.Raycast(ray, out hit, layerMask);
        Debug.Log(string.Format("mask == {0}, gotHit == {1}, hit == {2}",
            layerMask.value, gotHit, hit.collider));

… and attached it to my MainCamera. However, regardless of what I set as HitMe.layerMask, I get hits when I hover my mouse over any of these 3 objects (even though I suspect, that mask set to Cylinder should give me only Cylinder, and NULL, when I hover Sphere, or Box, or nothing).

How come?

Here is link to full project.

So here’s your problem. What you think is your layerMask parameter is actually your maxDistance parameter. Based on your arguments, this is the method you’re calling:

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

LayerMasks can be implicitly converted to ints, which can be implicitly converted to floats.

Change your function call to this and it should work as expected:

bool gotHit = Physics.Raycast( ray, out hit, Mathf.Infinity, layerMask );

If you’re using Visual Studio, you can mouseover the function name to see which overloaded function you’re actually calling.