- Home /
How to prevent a NavMeshAgent to constantly spin along its y axis?
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.
You need to use agent.SetDestination(destination)
because if you just set the destination variable, it won't recalculate his path. Which could easily lead to some problems, especially if there are moving objects in the scene.
Also, I found that this works better than checking the agents distance.
void Update()
{
if (Vector3.Distance(transform.position, agent.destination) < 0.1f)
{
GotoNextPoint();
}
}
I applied what you suggested, but unfortunately the NPC behavior remains the same
Well it could be your code at setting the point. You should linearly walk the nodes ins$$anonymous$$d of what your doing. Like this;
private int currentDestination;
public Transform[] destinations;
void SetNextPoint()
{
if (currentDestination +1 < destinations.Length)
{
currentDestination+=1;
}
else
{
currentDestination = 0;
}
agent.SetDestination(currentDestination);
}
Alternatively, if you want him to walk back the exact path he came...
private int currentDestination;
public Transform[] destinations;
private bool direction;
void SetNextPoint()
{
if (direction)
{
if (currentDestination +1 < destinations.Length)
{
currentDestination+=1;
}
else
{
direction = false;
currentDestination -=1;
}
}
else
{
if (currentDestination -1 > -1)
{
currentDestination-=1;
}
else
{
direction = true;
currentDestination +=1;
}
}
agent.SetDestination(currentDestination);
}
Answer by suIly · Mar 17, 2018 at 11:48 PM
Add a rigid body to the object and add a constraint to the y rotation.
It's a bit better, thanks! However, I discovered that my issue derived from my Nav$$anonymous$$eshSurface.