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.