Hello, I’m working on a project right now where all of our game board objects are parented under a single Game Object that we move around (rather than moving the camera) as the player navigates the world. We’re using an APP Grid Graph for our pathfinding, but we’re running into an issue where, even if we parent the APP script object under the game board Game Object that we’re moving, the graph never moves with it. We need to update the pathfinding graph at runtime due to players being able to rearrange decorations in the world, and those updates are always ending up incorrect due to all of the world objects having moved in world space.
Is there any way to force this sort of usage to work? Currently we’re relocating all of the nodes before we update the graph and trying to get that usage to work, but it seems incredibly inefficient to do that and possibly incorrect if we aren’t able to correctly offset the paths returned by the Seeker. Is there any way at all to get the graph to move along with the world as we need it to here?
Cheers!
Shane
Hi
There is no support for this built in, however I think you should be able to make it work using something like this.
-
In the GridGraph, add a field
public Matrix4x4 worldOffsetMatrix; // Or some other name
-
In the UpdateNodePositionCollision method, change this line
Vector3 position = collision.CheckHeight((Vector3)node.position, out hit, out walkable);
to
Vector3 nodePosWorld = worldOffsetMatrix.MultiplyPoint3x4((Vector3)node.position);
Vector3 position = worldOffsetMatrix.inverse.MultiplyPoint3x4(collision.CheckHeight(nodePosWorld, out hit, out walkable));
// Potentially you want to cache the matrix inverse calculation
and
node.Walkable = walkable && collision.Check((Vector3)node.position);
to
node.Walkable = walkable && collision.Check(nodePosWorld);
I haven’t tested this myself, but I think it will work.
Unfortunately, it doesn’t appear to have worked, but it is somewhat difficult to tell exactly because the grid visualization, understandably, doesn’t line up with the world once it’s moved. We have an alternate solution whereby when the graph needs updated, we move the gameboard object back to the origin and do something to this effect:
AstarPath.active.FlushWorkItems(true, true); // flush any current work items
PDGameboardManager.Instance.PushGameboardPosition(); // reset the gameboard to 0,0
AstarPath.active.UpdateGraphs(graphUpdate); // queue the graph update
AstarPath.active.FlushGraphUpdates(); // force the graph update
PDGameboardManager.Instance.PopGameboardPosition(); // reset the gameboard position to wherever it was
I know that sort of chunks the pathfinding in terms of performance, but it’s not often that we would collide with a lot of pathfinding updates occurring. Does that seem like a reasonable work around?