No LayerMasks in Physics.Raycast ?

Is there no way of passing a layerMask to a Raycast?

Physics.Raycast (ray.origin, ray.direction, hit)

If I add a layerMask in there, I get an error saying no overload blah blah…

I need to do a raycast down from mouse screen position but I need my raycast to ignore certain colliders (layers)…

Have you seen the Raycast docs? It’s definitely there.

Cheers,
-Jon

Yes, but try implementing it… doesn’t work… get a Unity error instead.

this :-

if (Physics.Raycast (ray.origin, ray.direction, hit, lMask)) {

produces this :-

Assets/Scripts/Camera/ObjectSelector.js(294,29): BCE0023: No appropriate version of 'UnityEngine.Physics.Raycast' for the argument list '(UnityEngine.Vector3, UnityEngine.Vector3, UnityEngine.RaycastHit, UnityEngine.LayerMask)' was found.

Look at the docs a bit more closely: “layerMask : int = kDefaultRaycastLayers”. It takes an integer, so you use LayerMask.value. Or just an int, if you understand layermasks.

–Eric

Trust me, it works. :slight_smile: You need to include the other parameters (distance) to the function call. And I believe the LayerMask implicitly converts to int.

if (Physics.Raycast (ray.origin, ray.direction, hit, Mathf.Infinity, layerMaskInt)) {

Cheers,
-Jon

Ahh… Legend! Thanks Jon :slight_smile: