Hi. I’m creating a group of paths to work as a patrol route for an AI, but there’s a slight problem with the paths. My game world (including the graph) is procedurally generated at runtime, so a grid graph works easiest for me. This is how I create the graph:
var data = AstarPath.active.astarData;
var graph = data.AddGraph(typeof(GridGraph)) as GridGraph;
graph.center = new Vector3(genWidth / 2f, 0f, genHeight / 2f) * UnitSize;
graph.width = genWidth * UnitSize;
graph.depth = genHeight * UnitSize;
graph.cutCorners = false;
graph.collision = new GraphCollision() { heightMask = ~(1 << wallMask), mask = 1 << wallMask };
graph.UpdateSizeFromWidthDepth();
AstarPath.active.Scan();
genWidth
and genHeight
are the dimensions of the generated map in game-units, and UnitSize
is the size of one game-unit in Unity units (in this case, 3f
). This is how I create the paths:
for (var i = 0; i < patrolPoints.Length; i++)
{
var point = patrolPoints[i];
var nextPoint = patrolPoints[(i + 1) % patrolPoints.Length];
var path = ABPath.Construct(point, nextPoint);
AstarPath.StartPath(path);
AstarPath.WaitForPath(path);
paths.Insert(i, path);
}
patrolPoints
is an array of Vector3s, which defines the points the AI should be patrolling through. paths
is a list of the created paths. The previously mentioned problem is that the paths are slightly misaligned from the patrol points:
The vertical red line on the right is one of the patrol points, and the red and black lines are visualisations of the paths that use the patrol point. As you can see, the paths don’t actually go through the point, but through a node next to it. This happens with all the points, and the relative direction for the actual node the path uses varies from point to point, so a hacky solution of just offsetting the paths to cancel the misalignment wouldn’t work.
I’m using version 3.8.2 of the A* Pathfinding Project and Unity 5.3.4f1.
Thanks for any help!