- Home /
My raycast ignores layer mask, could you give me some advice?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour { public Camera cam;
public LayerMask movementMask;
public Interactable focus;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, movementMask))
{
Debug.Log("We hit" + hit.collider.name + " " + hit.point);
}
Answer by xAdamQ · Aug 28, 2019 at 12:21 PM
the third parameter is maxDistance not layer, you have to edit it and write maxDistance then layerMask.
You only actually want to write "maxDistance" if you've defined that as a float variable. What you may actually want to do in this case (to preserve the intent of the code in the question) is write "$$anonymous$$athf.Infinity" for the maxDistance. That's the default when you don't provide a max distance, which you haven't.
See (final entry on...) https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
Thanks guys. I got frustrated after few days of working in unity so i totally forgot about that.