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 *= path.transform.GetChild(i);*
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 *= path.transform.GetChild(i);*
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!
yep I just checked its definitely because the "words" text file is not linked to the script I also got these debug errors it clearly states the object is not set to a reference in file and it refers to the debug quote checks you stated in the script so even the debug check isn't linked I will contact u in the forum private messaging it wont let me post images here I can easily show u the errors expect a private message form me in the forum area
yep I just checked its definitely because the "words" text file is not linked to the script I also got these debug errors it clearly states the object is not set to a reference in file and it refers to the debug quote checks you stated in the script so even the debug check isn't linked I will contact u in the forum private messaging it wont let me post images here I can easily show u the errors expect a private message form me in the forum area
– sid4