I’m building a TD game and need to check if a build location is valid (ie. will it block the path from spawn to base?) before letting the player build there.
I’m using Aron Granberg’s a* project 3.2.5 pro
In the image, my start location is the yellow circle on the left, destination is the magenta circle on the right. Red are unwalkable. Max search size is set to the same size as my grid nodes.
In the middle of the plane are two cubes acting as walls through which only one grid point is open. This is where i’m testing the function.
As other posts have mentioned as well as the docs, I’m using the UpdateGraphsNoBlock() method, which returns True if the path is valid False if it is not.
Problem is it never changes.
If my path from spawn to base is blocked at startup it always returns false. (this is expected)
If my path is valid at startup like the picture shows, the method always returns true, even after blocking the 1 unit between the walls.
public void QualifyPath(Vector3 startPoint, Vector3 endPoint, Vector3 blockPoint)
{
Node start = AstarPath.active.GetNearest(startPoint).node; // this is my enemy spawn location
Node end = AstarPath.active.GetNearest(endPoint).node; // this is my base that the enemy must travel to
Node block = AstarPath.active.GetNearest(blockPoint).node; // this is the proposed build site for a new tower
Vector3 blockPos = new Vector3(block.position.x, block.position.y, block.position.z) * Int3.PrecisionFactor; // Get the node's position in world space to be blocked
Bounds checkBounds = new Bounds(blockPos, Vector3.one * AstarPath.active.astarData.gridGraph.nodeSize); // Create Unity.Bounds object to feed into GraphUpdateObject
bool passTest = GraphUpdateUtilities.UpdateGraphsNoBlock(new GraphUpdateObject(checkBounds), start, end, true); // Run the check to see if the block object will block the path.
if (passTest)
Debug.Log("PASS");
else
Debug.Log("FAIL");
// Everything after this is just for debug purposes...
// debugObject is a unit cube i use to give me a visual check of my checkBounds
// Destroy the old debug object if any
if (debugObject != null)
DestroyImmediate(debugObject);
//I intantiate it at the position of my checkBounds
debugObject = GameObject.Instantiate(Managers.prefabManager.debugObject, checkBounds.center, Quaternion.identity) as GameObject;
// Set scale to match checkBounds
debugObject.transform.localScale = checkBounds.size;
}
My debug object always shows in the correct spot, I’ve even tried setting the bounds size much higher than one unit to see if it wasn’t large enough to block the path… still no go.
Regardless of the node I check the answer is the same, like its not updating the graph before the check.
The only thing I can see related to the other posts on this subject, is that I am creating the GUO (graph update object) and Bounds for it in code rather than reading it from an existing gameobject, but I’ve seen nothing in the docs that require this.
Any thoughts?
Thanks.