NPC to travel to destination while avoiding obstacles
The introduction of Unity's NavMeshAgent has greatly simplified the coding for NPC (Non-Player Character) and enemy agent behaviors. In this recipe, we'll add some wall obstacles (scaled cubes), and generate aNavMesh so that Unity knows not to try to walk through walls. We'll then add aNavMeshAgentcomponent 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
panel is visible, then the Scene
panel 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:

Getting ready
The required Terrain Texture
SandAlbedo
can be found in the 15_01 folder. Alternatively, you can go to Assets |
Import Package | Environments
, deselect everything, and then locate and tick this asset:Assets/Environment/TerrainAssets/SurfaceTextures/SandAlbedo.psd...