This is where I got so far. The code below generates a GridGraph based on my terrain.
var graph = (GridGraph) AstarPath.active.astarData.AddGraph(typeof(GridGraph)); graph.width = width; graph.depth = depth; graph.center = GetTerrainCenter(); graph.nodeSize = 1f; graph.UpdateSizeFromWidthDepth(); graph.collision.heightMask = LayerMask.GetMask("Terrain"); AstarPath.active.Scan();
The question I have now is as follows. I want to use the Tags feature of this package by marking water-covered parts of my terrain unwalkable for my units. I understand there is an UpdateGraphScene script but since I’m doing this procedurally I can’t use it.
AstarPath marks everything as walkable, including water (or rather “underwater”, because essentially raycasts hit the bottom of the water bodies), because the collision mask is set to the terrain layer; thus, my water plane, which is on the Water layer, is ignored. If I set the layer mask to include water and terrain, the only thing that changes is that raycasts start hitting the surface of water, but it’s walkable all the same.
http://arongranberg.com/astar/docs/graph-updates.php says for graph updates (and setting walkability is an update of a graph, essentially) done procedurally I’m supposed to use GraphUpdateObject and override the Apply(GraphNode node) function. This is of little use, however, because GraphNode contains no information about the layer that the original height testing raycast landed on. I do have access to node.position, of course, so I could do another raycast and see what it hits (hit.transform.gameObject.layer), but I wonder if there is a more efficient way of doing this.