Recast graph on a terrain

Example scene with a terrain and a recast graph.

This example scene demonstrates how to use a recast graph on a Unity terrain. It also demonstrates how to use tags to make the agent prefer moving on the dirt road instead of on the grass.

In the scene, you can move the cursor around, and the agent will move to that position.

Contents

Graph setup

The recast graph works pretty much out of the box on Unity terrains. The only thing you need to ensure is that the graph has the rasterize terrain option enabled.

The recast graph converts the terrain into a mesh before rasterizing it. To improve performance when rasterizing the terrain, the heightmap is downsampled. You can adjust how much the recast graph should downsample the heightmap using the Terrain Heightmap Downsampling Factor field. Lower values will result in a more accurate graph. Using higher values can speed up scanning a bit, and is of particular importance if you want to do graph updates at runtime.

Any trees on the terrain will be automatically detected and added to the graph. However, only colliders will be rasterized on trees. Rasterizing the meshes in tree prefabs is not supported. This is usually what you want, since rasterizing all meshes on all trees can very quickly make the scan take a very long time. Colliders on trees will be rasterized regardless of what the rasterize colliders field is set to.

Making the agent prefer the dirt road

The agent in the scene will prefer to move on the dirt road, since it has a lower cost than the grass around it.

To achieve this, the recast graph has been configured with a per-terrain layer modification (see screenshot in previous section) that marks the graph as having the "Dirt Road" tag everywhere the dirt texture has been painted.

The FollowerEntity component on the agent has been configured such that the "Basic Ground" tag has a cost per world unit of 2000, but the "Dirt Road" tag has a cost per world unit of only 1000. This means it is twice as costly to move on the grass than on the dirt road. You configure this under the Tags foldout in the FollowerEntity component, or in the Seeker component if you are using that instead.