Apologies for the late post. This week we looked at simple AI navigation.
The basis of navigation in Unity is the NavMesh. A NavMesh is a simplified view of the game world that marks out all the areas that AI characters can move through. The pictures below show the same scene with the NavMesh made visible in the second:
The blue areas are the NavMesh and are the areas that AI characters can move in. The holes made by the walls are apparent as is the “ramp” on the steps and the circle/arrow combinations where characters can jump down from or across obstacles.
Making a NavMesh
The first step in making a NavMesh is marking pieces of the scene as “walkable” and “not walkable”. Open the Navigation panel from the Window menu. When ever this panel is selected, the NavMesh will be displayed in the Scene View, by default.
The Navigation panel has three buttons across the top. Select “Object” and select objects in the Hierarchy that you want to be part of the NavMesh. Select the floor or ground object, select the “Navigation Static” option and set “Navigation Area” to “Walkable”:
Then select each wall/obstacle and again select “Navigation Static” but this time set “Navigation Area” to “Not Walkable”.
Press the “Bake” button in the bottom of the panel to create the NavMesh.
Adding a Character
We use the AIThirdPersonController prefab from the Unity Standard Assets to test our NavMesh. Adding it to our scene, we find that it has a “AI Character Control” script that takes a “Target” parameter.
Just drag any game object (even an empty one) from the Hierarchy here to tell the character to move to it.
The sample project can be found here. This is in Unity 5.5, so ensure you’ve upgraded to this version before trying to open it. The sample project also contains some logic to reroute the character between an array of targets, alternating when the spacebar is pressed. It also has had some “Off Mesh Links” generated. These allow the character to drop from the elevated platform and to “jump” (more accurately float really with this character) between them.