How to make a raycast stop if it hits a collider that is not the one its looking for before it gets to the one its looking for?


My raycast is shooting from the screen after a mouse click. Its finding objects in the “Face” layer, as I want it to, but I would like it to stop if it runs into a collider on an object from any other layer first. Any help is much appreciated.

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;

if (Physics.Raycast(ray, out hit, 1000, 1 << LayerMask.NameToLayer("Faces")))
     //do stuff


Just don’t search in a particular layer. Use another overloaded method that does not have a LayerMask as one of its parameters.

Here is the one you want: 137463-screenshot-2.png

Here is one with a distance parameter: 137464-screenshot-3.png

thank you!

If I wanted to selectively ignore only certain specific layers. How would I do that?