AIPath.Scan() high gc allocation switching to detecting runtime obstacle doesnt work

As mentioned in the title, I am currently monitoring performance in my game by executing AIPath.Scan() for each agent before setting its destination. This ensures that all objects instantiated at runtime are recognized and avoided in the path calculation.

However, I’ve noticed that there’s always a short stutter every time AIPath.Scan() is executed.

I attempted to use the method demonstrated in the sample scene with the layered grid, where pressing ‘P’ spawns an obstacle. I modified my AI to avoid obstacles at runtime without needing to perform a scan each time, if I understood correctly. Despite this, the agents still ignore the obstacle, even though it is on the “TransparentFX” layer, and the spawned object is on the same layer.

Here are a few screenshots:





Hi

Is there a reason you need to scan before every single path calculation? That seems awfully wastful.

Spawning obstacles is usually handled using runtime graph updates, instead. See Graph Updates - A* Pathfinding Project

Note that your collision testing shape is a sphere raised up quite a bit from the ground (see the preview in the grid graph settings). So if the obstacle is close to the ground and not very tall, it might not be detected.

It’s a tycoon game and player can spawn in objects which are there then until they upgrade them. I’ve done it now with graph update and it works good. Thank you. If that object gets how to free again that space so its walkable again?

I’ve implemented it like that:

public void RequestGraphUpdate(Bounds bounds)
{
    Bounds originalBounds = bounds;
    Vector3 sizeReduction = new Vector3(0.25f, 0.25f, 0.25f);
    Bounds adjustedBounds = new Bounds(originalBounds.center, Vector3.Scale(originalBounds.size, sizeReduction));

    StartCoroutine(UpdateGraphDelayed(adjustedBounds));
}

WaitForSeconds twoSeconds = new WaitForSeconds(2);
public IEnumerator UpdateGraphDelayed(Bounds bounds)
{
    yield return new WaitForFixedUpdate();
    yield return twoSeconds;

    var guo = new GraphUpdateObject(bounds)
    {
        modifyWalkability = true,
        setWalkability = false
    };
    AstarPath.active.UpdateGraphs(guo);
}

Usually I recommend just doing

    var guo = new GraphUpdateObject(bounds);
    AstarPath.active.UpdateGraphs(guo);

That will re-recalculate only that part of the graph from scratch. As long as your objects have colliders and such configured appropriately, it will detect the new objects as obstacles.
When you remove the objects, you just re-run that exact same code, and now it will detect no obstacles, so the ground will become walkable.