Itai Elidan
Itai Elidan

Reputation: 272

Raycast ignores all layers when trying to exclude just one

I have the following raycast:

Ray ray1 = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit1;
if (Physics.Raycast(ray1, out hit1, ~layer))
{
     Debug.Log("Hit: " + hit1.collider.gameObject.name);
}

The layer variable is a public variable and in the inspector I set it to layer 8. I want to raycast to all layers except layer 8 which I want to ignore, but when I raycast as shown it ignores all the layers.

Can you help?

Upvotes: 0

Views: 997

Answers (1)

enigma
enigma

Reputation: 113

I think what you might be trying to do is following:

if (Physics.Raycast(ray1, out hit1, maxDistance, ~layer))
{
     Debug.Log("Hit: " + hit1.collider.gameObject.name);
}

The third parameter is suppose to be raycast distance. You can read more about it from documentation here: https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

Upvotes: 3

Related Questions