Enemy starts chasing when you are within distance

This commit is contained in:
Stedd 2022-07-24 23:08:14 +02:00
parent b53f64a8f9
commit 5c39557f24
1 changed files with 13 additions and 4 deletions

View File

@ -6,17 +6,26 @@ using UnityEngine.AI;
public class EnemyAI : MonoBehaviour
{
[SerializeField] Transform target;
NavMeshAgent navMeshAgent;
[SerializeField] float chaseRange = 5f;
private NavMeshAgent navMeshAgent;
// Start is called before the first frame update
void Start()
{
navMeshAgent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
navMeshAgent.SetDestination(target.position);
if (DistanceToTarget(target.position) < chaseRange)
{
navMeshAgent.SetDestination(target.position);
}
}
private float DistanceToTarget(Vector3 targetPosition)
{
return Vector3.Distance(gameObject.transform.position, targetPosition);
}
}