Enemy starts chasing when you are within distance
This commit is contained in:
parent
b53f64a8f9
commit
5c39557f24
|
@ -6,17 +6,26 @@ using UnityEngine.AI;
|
||||||
public class EnemyAI : MonoBehaviour
|
public class EnemyAI : MonoBehaviour
|
||||||
{
|
{
|
||||||
[SerializeField] Transform target;
|
[SerializeField] Transform target;
|
||||||
NavMeshAgent navMeshAgent;
|
[SerializeField] float chaseRange = 5f;
|
||||||
|
|
||||||
|
private NavMeshAgent navMeshAgent;
|
||||||
|
|
||||||
// Start is called before the first frame update
|
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
navMeshAgent = GetComponent<NavMeshAgent>();
|
navMeshAgent = GetComponent<NavMeshAgent>();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update is called once per frame
|
|
||||||
void Update()
|
void Update()
|
||||||
|
{
|
||||||
|
if (DistanceToTarget(target.position) < chaseRange)
|
||||||
{
|
{
|
||||||
navMeshAgent.SetDestination(target.position);
|
navMeshAgent.SetDestination(target.position);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private float DistanceToTarget(Vector3 targetPosition)
|
||||||
|
{
|
||||||
|
return Vector3.Distance(gameObject.transform.position, targetPosition);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue