The NavMeshAgent component that we added to the Sphere-arrow GameObject does most of the work for us. NavMeshAgent needs two things:
- A destination location to head toward
- A NavMesh so that it can plan a path and avoid obstacles
We created two possible waypoints as the locations for our NPC to move toward; that is, Capsule-waypoint-0 and Capsule-waypoint-3.
The C# script class called WaypointManager has one job: to return a reference to the next waypoint that our NPC should head toward. There are two variables, wayPoint0 and wayPoint3, that reference the two waypoint GameObjects in our scene. The NextWaypoint(...) method takes a single parameter named current, which is a reference to the current waypoint that the object is moving toward (or null). This method's task is to return a reference to the next waypoint that the NPC should travel toward. The logic for this method is simple: if current refers to waypoint0, then we'll return waypoint3; otherwise...