Raycast doesn't seem to work

I have an issue with my raycast for pointer clicks.

I set up a layerMask so that it only applies to my character but it doesn’t seem to register.

This is what i have currently:

public void OnPointerClick(PointerEventData evd)
{
if(evd.button == PointerEventData.InputButton.Left)
{
   if (Physics.Raycast(ray, out hit, layerMaskPlayer))
   {
       Debug.Log("Hit: "+hit.transform.gameObject);
   }
   else
   {
        Debug.Log("Miss");
   }
}
}

For some reason this doesn’t work it selected the floor beneath him. This is my layer mask in the editor that i setup:


My player is assigned to this Character layer. With the above setup, my ray cast debug log gives “Hit: Floor”, that is the object he is standing on which is not what i wanted.

So then i tried with these settings:

Then i don’t hit anything at all, i get “Miss” for my debug log.

My player has a capsule collider attached too.

Please help as i am totally lost on what is wrong here.

1 Like

You might need to do this

 if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMaskPlayer))

This has kicked me in the but a few times in the past. From what I see in the docs, there is no method overload for layermask being the 3rd parameter, but distance is. It might be thinking you are giving it a distance due to layermask having a implicit operator for int (meaning it can be treated as if it was an int).

1 Like

whats “ray” value? How did you calculated that?