Tell AI to immediately find a new path and use it

I want an AI to stop moving when the player closes a door and see if the AI can find another way into the room. My attempts to use “SearchPath()” have resulted in no difference in behavior. The AI can easily walk through a door if the player closes it too late. Is there a way to stop using a path and force a recalculation?

I’m using a 2D grid graph.

Hi

Do you update the graph immediately when the door is closed?

Calling SearchPath will make the agent start to search for a path, but that search is asynchronous, so it may take a few frames to complete. You can block until the search is complete using:

ai.SearchPath();
seeker.GetCurrentPath().BlockUntilCalculated();

This will make the AI update its path immediately in a single frame.

Hello,

I’ve been using the Dynamic Grid Obstacle on my door to update the graph. I tried following the documentation for updating 2D grids, but it resulted in strange graphs, so I added a DGO to my door.

After using your code, and changing the DGO’s check time to 0, it works perfectly. Am I correct in saying that having the check time be 0 makes it very expensive?

As for updating the graph directly, here’s the before and after closing/opening the door.

and this is the code attached to the door:

private void Update() {
    if (Input.GetKeyDown(KeyCode.RightControl)) {
        mesh.enabled = !mesh.enabled;
        col.enabled = !col.enabled;

        Bounds bounds = col.bounds;

        AstarPath.active.UpdateGraphs(bounds);
        EnemiesMaster.instance.recalculatePaths();
    }
}

It should refill that space completely, right?

Hi

Yeah it looks like the graph has been recalculated and the path updated properly, but your door is not blocking that space.
Are you sure that the door’s layer is included in the collision layer mask in the grid graph settings and that it is not a trigger?

Hello,

When I activate the door and its collider, it blocks the space perfectly. The rooms get separated into two different graphs. It’s just weird when it gets disabled and the graph tries to rebuild, it doesn’t add the entire space again. If the door is too thick, the graphs aren’t even reconnected.

When the door is closed:

When the door is too thick and is re-opened, the graph doesn’t calculate correctly:

Thank you for taking your time to help me out with this issue!

Hi

Ah, I think I know what the issue is.
When you call col.bounds on a disabled collider that will give you back a bounding box which has a zero size I think (for some reason Unity’s physics system does this). Try to grab the bounding box before you disable it and after you enable it.

Another solution that is probably easier is to simply attach the DynamicGridObstacle component to the door. Then you can remove your code to update the graph, though to recalculate the paths you may want to register to AstarPath.OnGraphsUpdated.

Hey,

Grabbing the bounds when it’s enabled works very smoothly. Thank you so much for your help!

1 Like