GraphUpdateShape not working

Hi, I’m trying to update the walkability of a complex (non-convex) area on a grid graph. The approach I’ve taken is to have a polygon collider to specify the the area that needs to be updated. I convert the colliders points from Vector2 to Vector3 by setting their z value to 0 (the grid graph position is at z=0) and then use these points to create a GraphUpdateShape. Here is the code:

Transform cachedTransform = transform;
Vector3 localPoint;

// Get the points from the collider and convert them to 3d
Vector3 points = new Vector3[_areaCollider.points.Length];
for (int i = 0; i < _areaCollider.points.Length; i++)
{
localPoint = new Vector3(_areaCollider.points[i].x, _areaCollider.points[i].y, 0);
// Convert the point from local space to world space and add to the array
points[i] = cachedTransform.TransformPoint(localPoint);
}

// Define a graph update shape based off the collider 3d points
GraphUpdateShape shape = new GraphUpdateShape();
shape.convex = false;
shape.points = points;

// Create a Graph update Object based on the GraphUpdateShape and modify the tag for the nodes
// within the shape
_updateObject = new GraphUpdateObject();
_updateObject.shape = shape;
_updateObject.trackChangedNodes = true;
_updateObject.modifyWalkability = true;
_updateObject.setWalkability = false;

// Apply the graph update
AstarPath.active.UpdateGraphs(_updateObject, 0);

It never detects any nodes within the shape. If I set the GraphUpdateObject to use the bounds of the collider, it works (but not as accurate of course). I have even tested using the bounds of the shape (which is exactly the same as the bounds of polygon collider) and not the shape itself, it still does not detect any nodes. It only seems to work if I do not use GraphUpdateShape at all.

Any suggestions or ideas as to what is going on?

Hi

The graph update shape class checks if they are inside the shape on the XZ plane, not the XY plane (that class was written before extensive 2D support was added to Unity). If your game is only 2D then it will not work that well.

Ah I see, that is a shame! I have written a function which uses GetNodesInArea() to get nodes inside the collider’s bounds and then does further checks to ensure the polygon collider overlaps the node’s position. This is working just fine, but I was hoping to use a built-in approach as it is probably better GC wise.

Thanks anyway!

Hi

What you can do is subclass the GraphUpdateObject and override the Apply method. Then you can apply any constraints that you want (it will always first check the bounds however as a performance optimization).

See http://arongranberg.com/astar/docs/graph-updates.php#graphupdateobject

1 Like

Oh cool, I’ll check that out, thanks!