Hi I am using A* pro and I am building a tower defence game and would like to know what can I do to check if a path between two vectors exists. (simple boolean)
This is used when i place towers to avoid new towers obstructing a path tot he goal.
Hi I am using A* pro and I am building a tower defence game and would like to know what can I do to check if a path between two vectors exists. (simple boolean)
This is used when i place towers to avoid new towers obstructing a path tot he goal.
Hi Guys after doing some research I found the way to do this
I am pasting my code as is on the solution this has to run in a coroutine in the update function , I know the coroutine can be optimized quite a bit but I just want to share , most explanations don’t show how to get the graphnode from the start and end point
/// <summary>
/// Check if there is a clear route from the start point to the end point
/// </summary>
/// <returns>The route.</returns>
private IEnumerator CheckRoute()
{ //wait for frame to end
yield return new WaitForEndOfFrame();
GraphNode startNode = AstarPath.active.GetNearest(start.transform.position, NNConstraint.Default).node;
GraphNode endNode = AstarPath.active.GetNearest(end.transform.position, NNConstraint.Default).node;
bool val = Pathfinding.PathUtilities.IsPathPossible(startNode,endNode);
//If we get a list that is empty there is no path, and we blocked the road
//Then remove the last added tower!
if (!val && shouldCheck)
{
Debug.Log("empty route delete tower");
if (towers.Count > 0)
{
GameObject g = towers[towers.Count - 1];
towers.RemoveAt(towers.Count - 1);
var towOIbj = g.GetComponent<Tower>();
towOIbj._towerPlaceTile.TowerPlaced = false;
Destroy(g);
shouldCheck= false;
LoadTowerModelInGhost(towOIbj.ModelConfig.GunType);
//AstarPath.active.Scan();
}
}
}
Hi
It seems you found a solution to your problem. But I just wanted to let you know there is a function which might make what you want to do a bit simpler: http://arongranberg.com/astar/docs/class_pathfinding_1_1_graph_update_utilities.php#af1d9912f395e3a1b5ef4f21d1f19da74
This function will update the graphs and return a boolean for if it blocked the path, otherwise (or optionally always) it will revert the graph to its previous state.