Rather than forcing a GameObject to follow a single rigid sequence of locations, we can make things more flexible by defining a WayPoint class where each waypoint GameObject has an array of possible destinations, and each of these has its own array. In this way, a directed graph (digraph) can be implemented, of which a linear sequence is just one possible instance.
To improve our game and make it work with a digraph of waypoints, do the following:
- Remove the scripted WayPointManager component from the Sphere-arrow GameObject.
- Replace the ArrowNPCMovement C# script class with the following code:
using UnityEngine;
using System.Collections;
public class ArrowNPCMovement : MonoBehaviour {
public Waypoint waypoint;
private bool firstWayPoint = true;
private NavMeshAgent navMeshAgent;
void Start (){
navMeshAgent = GetComponent<NavMeshAgent>();
HeadForNextWayPoint();
}
void Update () {
...