I’m loving A* Pathfinding - so much useful functionality!
I’m using a GridGraph, and quite often I’m getting “Searched whole area but could not find target” path errors. Often they’re really basic paths - the game world is very open, there are some slopes that need to be navigated around, but they’re relatively short. Some units are failing to find a path across open ground. It seems to be consistent, as in I can click on a specific spot and get this error each time I click, and then click on a nearby spot and get an immediately calculated path with everything working normally. After it starts moving, I can click on the first spot again, and it’ll often be able to find a path there without problems, presumably because it’s got a different starting point.
Looking at the graph, there’s nothing that should be blocking the units. I can see all the nodes are walkable, and I can see walkable edges. It’s obviously possible, since I can often get to the destination with a number of different clicks (often clicking further away just to get a valid path, then clicking back) - but in the end it reaches the exact point that was previously giving the error.
I’m using a procedurally generated terrain, and props with the “square obstacle”
I’m setting the units onto the navmesh with something like:
ai = GetComponent();
//ai.Teleport(transform.position, clearPath: true); return; // If I know they’re already somewhere walkable, because of procedural generation
var constraint = NNConstraint.None;
// Constrain the search to walkable nodes only
constraint.constrainWalkability = true;
constraint.walkable = true;
var info = AstarPath.active.GetNearest(transform.position, constraint);
var closestPoint = info.position; // I’d rather get the closest point, rather than the closest node, but this is good enough.
closestPoint += Vector3.up; // Lift by 1m, otherwise occasionally a unit will “fall through the ground”
ai.Teleport(closestPoint, clearPath: true);
Any hints would be greatly appreciated!
— Background, in case it’s useful —
We’ve recently swapped from using a Recast Graph to using a Grid Graph, because we’re using MapMagic2 to procedurally generate the game world as the player explores. The terrain doesn’t change shape during gameplay, but is only generated as the player approaches, so we can’t pre-bake the navmesh. We CAN bake the navmesh after the terrain is generated, either each time or the first time and cache or save it, but it doesn’t seem easy to incrementally update any of the graphs. I was having problems with the recast graph, where I couldn’t join new sections to the current navmesh, which is why I switched to the GridGraph.
It might be possible that I’m causing this problem, or can sidestep it by changing the type of graph or the way I’m initializing the units. I’m happy to do things differently if that’ll work better. I get the impression that both MapMagic 2 and Pathfinding are working really well, but making different assumptions about how we’ll use them, and I’m struggling to glue them together properly.
The GridGraph is correctly generating, and correctly rescanning as we move around the world - right now I’m just rescanning the whole grid, and I’ve looked at the procedural example, but for now I haven’t copied across the code to update just the edges like that. We’ve got other units in the world, so we’ll need a way to detect them so I can turn off their pathfinding as the grid moves away from them - ideas are welcome, otherwise I’ll write code to search for units in the area that’s being deactivated or just deactivate when they’re a certain distance away even if they’re still on the navmesh.
If there’s a way to generate a new “square” of navigation each time a square of terrain is generated, that would be perfect. Otherwise what I’m doing is working well enough, assuming I can figure out what’s causing this pathing issue.
I really liked the recast graph because it was able to tell me when the unit is at the “closest point” to the target - we have interactable items that are also obstacles, like a tree you can cut down, so it’s really useful to just set the target inside the tree and let the unit approach from any direction. That was nice, but I can find a manual solution using Grid Graph if needed.