32 lines
675 B
C#
32 lines
675 B
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
|
|
public class EnemyAI : MonoBehaviour
|
|
{
|
|
[SerializeField] Transform target;
|
|
[SerializeField] float chaseRange = 5f;
|
|
|
|
private NavMeshAgent navMeshAgent;
|
|
|
|
void Start()
|
|
{
|
|
navMeshAgent = GetComponent<NavMeshAgent>();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (DistanceToTarget(target.position) < chaseRange)
|
|
{
|
|
navMeshAgent.SetDestination(target.position);
|
|
}
|
|
}
|
|
|
|
private float DistanceToTarget(Vector3 targetPosition)
|
|
{
|
|
return Vector3.Distance(gameObject.transform.position, targetPosition);
|
|
}
|
|
|
|
}
|