- Home /
Make NPC travel to waypoint
I've been trying to make an NPC travel through a series of waypoints and stop at the end of an array.
I started by making the NPC target a single waypoint and move toward int however, i receive these errors when I run the game:
NullReferenceException: Object reference not set to an instance of an object NPCMove.Start () (at Assets/Scripts/AI/NPCMove.cs:19)
UnassignedReferenceException: The variable target of 'NPCMove' has not been assigned. You probably need to assign the target variable of the NPCMove script in the inspector. UnityEngine.Transform.get_position () NPCMove.Update () (at Assets/Scripts/AI/NPCMove.cs:24)
public class NPCMove : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
private Transform myTransform;
private Transform stopDistance;
void Awake(){
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("WayPoint");
target = go.transform;
}
// Update is called once per frame
void Update () {
Debug.DrawLine(target.position, myTransform.position, Color.black);
//look at
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
//move
myTransform.position += myTransform.forward *moveSpeed * Time.deltaTime;
}
Also, how would I make this use an array.
Does your WayPoint object exist at the time Start is called? Does it have the tag "WayPoint"?
Answer by Pecek · Apr 21, 2014 at 03:11 AM
There is no "gameObject.FindGameObjectWithTag", only "gameObject.FindWithTag". After that it should work(at least these errors should be gone).
To use an array you have to create one, and loop through it. You can get an array of GO's by
GameObject.FindGameObjectsWithTag()
Keep in mind this will return every GO with a certain tag which will break the system if you have multiple agents.
I found this really old thread called "A Waypoint Script Explained in Super Detail!" and followed the instructions but it doesn't move past the first waypoint.
public class NPC$$anonymous$$ove : $$anonymous$$onoBehaviour {
public float accel = 0.8f;
public float inertia = 0.9f;
public float speedLimit = 10.0f;
public float $$anonymous$$Speed = 1.0f;
public float stopTime = 1.0f;
public float rotationDamping = 6.0f;
public bool smoothRotation = true;
public Transform[] waypoints;
private float currentSpeed = 0.0f;
private int functionState = 0;
private bool accelState;
private bool slowState;
private Transform waypoint;
private int wpIndexPointer;
void Start(){
functionState = 0;
}
void Update(){
if(functionState == 0){
Accel();
}
if(functionState == 1){
StartCoroutine(Slow());
}
waypoint = waypoints[wpIndexPointer];
}
void Accel(){
if(accelState == false){
accelState = true;
slowState = false;
}
if(waypoint){
if(smoothRotation){
Quaternion rotation = Quaternion.LookRotation(waypoint.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotationDamping * Time.deltaTime);
}
}
currentSpeed = currentSpeed + accel * accel;
transform.Translate(0, 0, currentSpeed * Time.deltaTime);
if(currentSpeed >= speedLimit){
currentSpeed = speedLimit;
}
}
void OnTriggerEnter(){
functionState = 1;
wpIndexPointer++;
}
IEnumerator Slow(){
if(slowState == false){
accelState = false;
slowState = true;
}
currentSpeed = currentSpeed * inertia;
transform.Translate(0, 0, currentSpeed * Time.deltaTime);
if(currentSpeed <= $$anonymous$$Speed){
currentSpeed = 0.0f;
yield return new WaitForSeconds(stopTime);
functionState = 0;
}
}
}
I also get an index out of range error.