- Home /
Why is my navmesh enemy pathing not working?
So I have created this little script to take an array of transforms and make the enemy walk from point to point in circles.
It successfully adds the transforms to the array without any errors, however my character doesn't move at all, any ideas anyone?
using UnityEngine;
using System.Collections;
public class EnemyPathing : MonoBehaviour {
[SerializeField]
Animator anim;
[SerializeField]
NavMeshAgent nMA;
[SerializeField]
Transform[] pathPoints;
GameObject path;
int nr;
void Awake()
{
anim = GetComponent<Animator>();
nMA = GetComponent<NavMeshAgent>();
path = GameObject.Find(transform.name + "Path");
pathPoints = new Transform[path.transform.childCount];
for (int i = 0; i < path.transform.childCount; i++)
{
pathPoints[i] = path.transform.GetChild(i);
}
nMA.Resume();
}
void Update ()
{
if (nMA.remainingDistance > nMA.stoppingDistance)
{
nMA.destination = pathPoints[nr].position;
}
else if (nMA.remainingDistance <= nMA.stoppingDistance)
{
if(nr < pathPoints.Length)
{
nr++;
}
else
{
nr = 0;
}
nMA.Resume();
}
}
}
Thank you in advance
Answer by Nucky9 · Jun 12, 2016 at 10:06 PM
I managed to find a few problems. First, you never set the initial destination, so your agent never has a distance to test against your if statements. Also, your 'nr < pathPoints.Length' needs to be 'nr < (pathPoints.Length - 1)', since the index starts at 0, not 1.
Finally, I don't understand the point of the loop you are using to build your path (although I am pretty new at Unity, so I might be missing something). Right now, you can drag the waypoints directly into the navagent's inspector to build the path list. Do you want to build the list automatically instead? If so, what are you trying to build the list from, all objects that have 'path' in their name?
Anyway, assuming you populate the navagent's path list manually, the following code works to get the navagent looping through all the path points:
using UnityEngine;
using System.Collections;
public class EnemyPathing : MonoBehaviour
{
[SerializeField]
NavMeshAgent nMA;
[SerializeField]
Transform[] pathPoints;
// GameObject path;
int nr = 0;
void Awake()
{
nMA = GetComponent<NavMeshAgent>();
//path = GameObject.Find(transform.name + "Path");
//pathPoints = new Transform[path.transform.childCount];
//for (int i = 0; i < path.transform.childCount; i++)
//{
// pathPoints[i] = path.transform.GetChild(i);
//}
nMA.destination = pathPoints[nr].transform.position;
}
void Update()
{
if (nMA.remainingDistance <= nMA.stoppingDistance)
{
if (nr < (pathPoints.Length - 1))
{
nr++;
nMA.destination = pathPoints[nr].transform.position;
}
else
{
nr = 0;
nMA.destination = pathPoints[nr].transform.position;
}
}
}
}
Hope that helps!
Thanks a lot!
As always the problem seems to be that I'm apparently blind... Anyway as for the initial loop, it was a simple setup to get ready to automate the process if i decided to change/add paths later.
To anyone who might need it Here is the complete script with a few comments for understanding
using UnityEngine;
using System.Collections;
public class EnemyPathing : $$anonymous$$onoBehaviour {
[SerializeField]
Animator anim;
[SerializeField]
Nav$$anonymous$$eshAgent n$$anonymous$$A;
[SerializeField]
Transform[] pathPoints;
GameObject path;
int nr;
void Awake()
{
anim = GetComponent<Animator>();
n$$anonymous$$A = GetComponent<Nav$$anonymous$$eshAgent>();
//Find the holder of the path, in my case
//the script is on a prefab called Enemy,
//thus the path-holder should be named "EnemyPath"
//Just to keep myself systematic
path = GameObject.Find(transform.name + "Path");
//I loop through each child in the path-holder
//each child is an empty gameobject
//This is to keep levelcreation fast and easy to understand
pathPoints = new Transform[path.transform.childCount];
for (int i = 0; i < path.transform.childCount; i++)
{
pathPoints[i] = path.transform.GetChild(i);
}
//First destination is set to the first child
//This is since the int will default to 0
n$$anonymous$$A.destination = pathPoints[nr].position;
n$$anonymous$$A.Resume();
}
void Update ()
{
//This were we move
//As long as we haven't reached our destination yet
//we'll move towards it
//Once we reach it, the next pathpoint is chosen
//Or we choose the first point again to circle
if (n$$anonymous$$A.remainingDistance <= n$$anonymous$$A.stoppingDistance)
{
if (nr < (pathPoints.Length - 1))
{
nr++;
n$$anonymous$$A.destination = pathPoints[nr].transform.position;
}
else
{
nr = 0;
n$$anonymous$$A.destination = pathPoints[nr].transform.position;
}
}
}
}
By any means feel free to use it
Your answer
