LayerMask not detected in Physics.Raycast

Hi all, I’m fairly new to Unity and I’m working on a movement system to move a unit from one location to another on a 3d object (map/mesh filter) that I’ve created. As far as I can tell, the LayerMask parameter of Physics.Raycast is holding up my code. The code runs fine up until the Physics.Raycast, where it doesn’t run any further. No error messages or anything. I’ve baked the navigation of the object that it’s moving on as well.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UnitCommander : MonoBehaviour
{
    public GameObject selectionMarkerPrefab;
    public LayerMask layerMask;

    // components
    private UnitSelection unitSelection;
    private Camera cam;

    void Awake ()
    {
        // get the components
        unitSelection = GetComponent<UnitSelection>();
        cam = Camera.main;
    }

    void Update ()
    {

        // did we press down our right mouse button and do we have units selected?
        if (Input.GetMouseButtonDown(1) && unitSelection.HasUnitsSelected())
        {
          
            // shoot a raycast from our mouse, to see what we hit
            Ray ray = cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            // cache the selected units in an array
            Unit[] selectedUnits = unitSelection.GetSelectedUnits();

            // shoot the raycast
            if (Physics.Raycast(ray, out hit, 100, layerMask))
            {
//It never arrives here
                Debug.LogWarning("Here");
                // are we clicking on the ground?
                if (hit.collider.CompareTag("Sea"))
                {

                    UnitsMoveToPosition(hit.point, selectedUnits);
                    CreateSelectionMarker(hit.point);
                }
            }
        }
    }

    // called when we command units to move somewhere
    void UnitsMoveToPosition (Vector3 movePos, Unit[] units)
    {
        Vector3[] destinations = UnitMover.GetUnitGroupDestinations(movePos, units.Length, 2);

        for(int x = 0; x < units.Length; x++)
        {
            units[x].MoveToPosition(destinations[x]);
        }
    }

    // creates a new selection marker visual at the given position
    void CreateSelectionMarker (Vector3 pos)
    {
        Instantiate(selectionMarkerPrefab, new Vector3(pos.x, 0.01f, pos.z), Quaternion.identity);
    }
}

The settings for the object that my unit is moving on can be viewed in the attached image.

Any help is appreciated.

Have you checked the previous if statement condition?

if (Input.GetMouseButtonDown(1) && unitSelection.HasUnitsSelected()) ...

This if statement is working. It stops working at: if (Physics.Raycast(ray, out hit, 100, layerMask))

Ok.

The ray appears to be fine, the Raycast too (assuming a distance of 100 is enough).

  • Does the mask (inspector) include “Sea”?
  • Since you are printing a warning …
Debug.LogWarning("Here");

… Do you have the warning indicator enabled in the console?
8448683--1120601--upload_2022-9-18_19-49-30.png
… it happens all the time

I found that the issue was that my MeshLayer game object with the “Sea” tag was nested within an empty game object. I moved it outside of the empty game object and the code is now working. Thanks for your help!