ABSTRACT

There are many ways to achieve pathfinding in Unity-most of them require “manual coding.” However, Unity ships with a native or “built-in” pathfinding system that is especially suited for 3D games, called Navigation Meshes. With this mechanism, pathfinding rests on the generation of a Navigation Mesh, which is an internal mesh whose faces define the walkable regions of an environment. “Walkable” here refers only to areas where intelligent characters (agents) could move, if they needed or wanted to. Building a Navigation Mesh is simple, provided you have an environment and level made. To start, make sure your level is marked as Navigation Static. To do this, select all environment objects, and then click the Static drop-down from the Object Inspector, and choose Navigation Static. 1

Then open the Navigation window, by choosing Window > Navigation from the application menu. Then select the Bake tab, and click the Bake button to generate the Navigation Mesh. When you do this, Unity projects a ray from the top downward, tracing all intersections with navigation objects to create a horizontally aligned map of the floor, constituting the Navigation Mesh. This appears in blue, inside the Scene Viewport when it’s not maximized, provided the Navmesh Display is enabled. You can toggle navmesh display using the Show NavMesh check box. 2

After generating the Navigation Mesh and inspecting it in the Scene Viewport, it’s likely you’ll spot some “problems.” Specifically, your mesh will be different from the true floor in two important ways. First, the Navigation Mesh will be “thinned out” or offset from the real walls. And inside thinner regions, like doorways, the mesh may even be broken entirely-disconnecting the outside of rooms from the inside. Disconnections like this are “bad” because agents cannot, by default, cross or travel over areas not covered by the Navigation Mesh. 3

You can fix this problem by reducing the Agent Radius setting from the Navigation window. This defines the total radius of an imaginary circle offset against the wall and surrounding the agent. Larger values result in wider gaps between the Navigation Mesh and the wall. This setting is important because it represents how close to a wall an agent can move while traversing the Navigation Mesh.