I set up my code a bit differently for the challenge and it works, but is there any drawback to the way I did it? I added the unitLayerMask variable to the MouseWorld script along with this method:
public static Unit GetUnit()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out RaycastHit rayCastHit, float.MaxValue, instance.unitLayerMask);
if(rayCastHit.transform.TryGetComponent<Unit>(out Unit unit))
{
return unit;
}
return null;
}
Then, in the UnitActionSystem script, I have:
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
HandleUnitSelection();
}
if (Input.GetMouseButtonDown(1))
{
selectedUnit.Move(MouseWorld.GetPosition());
}
}
private void HandleUnitSelection()
{
Unit unit = MouseWorld.GetUnit();
if (unit != null)
{
selectedUnit = unit;
}
}