The introduction of Unity's NavMeshAgent has greatly simplified the coding for non-player character (NPC) and enemy agent behaviors. In this recipe, we'll add some wall obstacles (scaled cubes) and generate a NavMesh so that Unity knows not to try to walk through walls. We'll then add a NavMeshAgent component to our NPC GameObject and tell it to head to a stated destination location by intelligently planning and following a path while avoiding the wall obstacles.
When the Navigation window is visible, the Scene window displays the blue-shaded walkable areas, as well as unshaded, non-walkable areas at the edge of the terrain and around each of the two wall objects:
Figure 10.1 – Example of an NPC avoiding obstacles