I’m trying to get simple programatic 2D room generation working with pathfinding.
I have the pathfinder object set up for 2D grid with 2D physics collision detection (circle collider) as shown in tutorials. (I also have the AI and target set up and these work and pathfinding correctly).
I can create a room in the editor with floor and wall tiles, press ‘scan’ and it displays the walkable / non walkable areas ands connections as expected with the floor walkable and some no walkable areas around the walls.
However When I generate the same room programatically (also resizing it as needed) I have to manually call tell each node if its walkable or not and this results in a slightly different result to clicking ‘scan’. (If I click scan after programatically generating the room the graph will change).
I build the room with prefabs laid out on a grid, with each prefab having a collider and GraphUpdateScene script. I then use the code below to try to update the pathfinder.
AstarData data = AstarPath.active.data;
var gg = AstarPath.active.data.gridGraph;
gg.SetDimensions( _width, _depth, _nodeSize);
gg.center = new Vector3(_width / 2 , -(_depth / 2), 0);
AstarPath.active.AddWorkItem(new AstarWorkItem(ctx =>
{
gg.GetNodes(node =>
{
// get the gameobject tile at the node position
var tile = _lm.GetTileAt((Vector3)node.position);
// see if the tile was set to walkable when created in its data (floor tile = walkable)
node.Walkable = tile != null && tile.GetComponent<Tile>().Walkable;
gg.CalculateConnections((GridNodeBase) node);
});
}));
// Scans all graphs
AstarPath.active.Scan();
But I know this isnt right as it doesnt seem to be using colliders to check whats walkable any more and isnt giving the same results as scan does with its connections. Also if I try creating a new grid it always ends up 90 degrees out in the editor even with 2D set so I ditched that approached.
Can you show me what I’m doing wrong please? I really simple code snipet is what I’m after.
What is the desired behavior? Do you want to set the walkability of the nodes manually or do you want to use the result that the grid graph scan gives you? Right now you seem to be trying to do both and I’m not sure what you actually want.
For now I’d like just a quick and easy ‘just what the grid graph scan gives me’ as i’m just prototyping really. I am in interested to know what was wrong with the manual method though or if there is a simple example somewhere in the docs for setting manually.
If you just want to scan the graph then what you are doing by calling AstarPath.active.Scan should work fine (without your work item). Is something not working with that?
Are you sure the tilemap has been updated when you scan the graph?
I think it’s possible that tilemaps do some kind of deferred update. Try to wait for 1 frame before you scan the graph.
Also. Regarding the 1 node border around your walls. You can reduce your Grid Graph -> Collision Testing -> Diameter setting to 0.99 or something which will ensure that the circle that is used for collision testing doesn’t touch the walls when it shouldn’t. Alternatively you can change the Collider Type field to Point.