NPC NavMeshAgent to follow waypoints in sequence
Waypoints are often used as guides to help autonomously moving NPCs and enemies follow a path in a general way but still be able to respond with other directional behaviors, such as fleeing or sensing if friends/predators/prey are nearby. The waypoints are arranged in a sequence so that when the character reaches or gets close to a waypoint, it will select the next waypoint in the sequence as the target location to move toward. This recipe will demonstrate an arrow object moving toward a waypoint. Then, when it gets close enough, it will choose the next waypoint in the sequence as the new target destination. When the last waypoint has been reached, it will start heading toward the first waypoint once more.
Since Unity’s NavMeshAgent has simplified coding NPC behavior, our work in this recipe basically becomes finding the position of the next waypoint and then telling NavMeshAgent that this waypoint is its new destination...