Files
TutorialRTS/Assets/Scripts/Unit.cs

47 lines
1.2 KiB
C#
Raw Normal View History

2026-03-19 11:57:23 +00:00
using UnityEngine;
2026-03-19 17:31:51 +00:00
using UnityEngine.AI;
2026-03-19 11:57:23 +00:00
public class Unit : MonoBehaviour
{
[Header("Components")]
public GameObject selectionVisual;
2026-03-19 17:31:51 +00:00
private NavMeshAgent navAgent;
private SelectionMarker selectionMarker;
void Awake()
{
//get the components
navAgent = GetComponent<NavMeshAgent>();
}
2026-03-19 11:57:23 +00:00
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
2026-03-19 17:31:51 +00:00
2026-03-19 11:57:23 +00:00
}
// Update is called once per frame
void Update()
{
2026-03-19 17:31:51 +00:00
if (selectionMarker != null && !navAgent.pathPending && navAgent.remainingDistance <= navAgent.stoppingDistance)
{
selectionMarker.DestroySelectionMarker();
selectionMarker = null;
}
2026-03-19 11:57:23 +00:00
}
public void ToggleSelectionVisual(bool selected)
{
selectionVisual.SetActive(selected);
}
2026-03-19 17:31:51 +00:00
public void MoveToPosition(Vector3 pos, SelectionMarker marker = null)
{
if (selectionMarker != null)
{
selectionMarker.DestroySelectionMarker();
selectionMarker = null;
}
navAgent.isStopped = false;
navAgent.SetDestination(pos);
selectionMarker = marker;
}
2026-03-19 11:57:23 +00:00
}