Hello everyone.
I have a script on an NPC that makes him patrol around a village. I’d like it to detect the player when it comes at a minimal distance and when it does, then direct himself to the player, but if the player gets away, i want it to return to it’s patrol.
I’m not sure where to start. Any script you know that would be a good start, or another thread you know… I’ve searched the forums but found nothing so far.
Here is the patrol script i have for now:
using UnityEngine;
using UnityEngine.AI;
using System.Collections;
public class Patrol : MonoBehaviour
{
public Transform[] pointsavisiter;
private int destPoint = 0;
private NavMeshAgent agent;
void Start()
{
agent = GetComponent<NavMeshAgent>();
agent.autoBraking = false;
VaAuProchainPoint();
}
void VaAuProchainPoint()
{
if (pointsavisiter.Length == 0)
return;
agent.destination = pointsavisiter[destPoint].position;
destPoint = (destPoint + 1) % pointsavisiter.Length;
}
void Update()
{
if (!agent.pathPending && agent.remainingDistance < 0.5f)
VaAuProchainPoint();
}
}
thanks a lot for your help!!
Seb