Support Forum

Providing my own 2D coordinates to ABPath instead of world positions

Hi, I am manually building my tiled grid graph for my 2D strategy game:

AstarData data = AstarPath.active.data;
GridGraph gg   = data.AddGraph(typeof(GridGraph)) as GridGraph;
gg.center      = Vector3.zero;
gg.SetDimensions(mapWidth, mapHeight, nodeSize:1);
AstarPath.active.Scan();
gg.GetNodes(n => {
    var node        = n as GridNode;
    node.Walkable   = IsWalkable(node.XCoordinateInGrid, node.ZCoordinateInGrid);
});
gg.GetNodes(node => gg.CalculateConnections((GridNodeBase)node));

Then I use ABPath:

var path = ABPath.Construct(start, end, null);
AstarPath.StartPath(path);
path.BlockUntilCalculated();

The first thing I noticed is that ABPath uses world positions, which makes sense most use cases, but in my case it’d be easier to just provide the X and Y coordinates of my 2D map (which is basically a bool[width,height]), and then get a path of nodes also based in such grid.

For the latter, that’s not too important since I can just do a mapping between the nodes into my grid’s cells. My main issue is the former, where I’d like to avoid providing world positions if possible.

Is there a way to do this, or do I need to stick to world positions?

Edit: I’m aware that I could rotate the grid, and adjust the center, and that should basically map the world positions to my grid correctly, but for some reason I don’t trust working with Unity’s world positions when it comes to a 2D grid tiled game :sweat_smile:

Hi

With the built-in ABPath this is not possible, however you could make your own subclass of ABPath and override the Prepare method. That would allow you to assign the start and end nodes instead of their world positions.