Texture-based Terrain Navigation

Hallo all.

I’ve been looking for a way to get my AI to navigate the terrain intelligently. I would like them to prioritize the use of certain textures over others when calculating a path.

I have roads textured on a Unity terrain. I need the AI to prefer the roads when going from place to place unless cross-country is called for (no roads near destination point) and even then avoid areas with rough or rocky ground.

The terrain is 5km square and uses only one splatmap. I currently use a baked Navmesh and AI have custom navigation and steering scripts alongside the Navmesh Agent.

A push in the right direction would be much appreciated.

UPDATE: Found this thread over in the forums dealing specifically with my problem

Navmesh layers

Seeing as its four years old, I’m hoping that there’d be something new for this. Otherwise, the method in the thread will do.

You can read the painted ground texture amounts directly with getAlphaMaps.

Suppose you have three ground textures and the 00 map corner is the first two blended evenly. Meaning, the paint numbers are [0.5, 0.5, 0]. The results of getAlphaMaps shows it as: AM[0,0,0]=0.5, AN[0,0,1]=0.5 and AM[0,0,2]=0. The coords are the “painting squares” (the ones you see in the brush,) which are not the same as heightmap squares (the conversion math is a bit tricky.)

I think the command used to be named getSplatMaps. There are more detailed code examples here in UA, and that name might help you find them.

The proper way to do this would be to read the splatmap layers of your terrain one-by-one (easy), and then generate a navmesh layer for each of them, then simply assign far greater costs for the non-path layers. It would only need to be done once. However it seems building navmesh layers at runtime is currently not possible. Which is ridiculous, considering this would be a simple script, nobody wants to build separate meshes for each layer of each terrain, and its how most people would do navigation on a vast terrain using unitys own terrain tools :confused: