I did this C# script and attached to my NPC, which is supposed to continuously patrol a maze. The agent seems to move correctly from one point to another, but sometimes, while changing its direction, it starts to spin along its y axis without stopping! I would like to prevent this behavior butI don’t know what it’s going wrong. This is the code of my script:
using UnityEngine;
using UnityEngine.AI;
using System.Collections;
public class MyChase : MonoBehaviour {
public Transform[] points;
private Transform play;
private int destPoint = 0;
private NavMeshAgent agent;
private GameObject pointsHolder;
private int numOfPoints;
Animator anim;
void Start () {
agent = GetComponent<NavMeshAgent>();
play = GameObject.FindWithTag("Player").transform;
anim = GetComponent<Animator> ();
agent.autoBraking = false;
pointsHolder = GameObject.FindGameObjectWithTag("PatrolPath");
numOfPoints = pointsHolder.transform.childCount;
points = new Transform[numOfPoints];
for (int t = 0; t < numOfPoints; t++) {
points [t] = pointsHolder.transform.GetChild (t).transform;
}
GotoNextPoint();
}
void GotoNextPoint() {
if (points.Length == 0)
return;
agent.destination = points[destPoint].position;
destPoint = (destPoint + 1) % points.Length;
}
void Update () {
if (!agent.pathPending && agent.remainingDistance < 0.5f)
GotoNextPoint();
}
}
P.s.: I have to add the animation after solving this problem, if you’re asking to yourself why I’ve declared an animator object.