Support Forum

SearchPath() doesn't consider the obstacle when instantiate

Hi,

I have played with this asset for a week and I have to say that it is amazing, even for me who is an hobbyist with no background in programming or game development. Everything was working like a charm until a I met my first issue.

The Scene:
I have a simple scene with about 30 agents on the right that have the following components: AIPath, Seeker, AI destination Setter, Simple Smooth, Raycast Modifier, RVO controller, Rigidbody 2D, circle collider 2D, Sprite renderer.

In the middle of the scene I have white squares which are my obstacles. They have the following components: Dynamic Grid Obstacle, Box collide 2D and Sprite Renderer.

At the left of my scene, I have targets for my agents.

The script:
I use a simple script (see below) which allows me to instantiate a wall on a grid when I click on the mouse.

The issue:
To illustrate my issue, I have set “AIPath/PathFinding/Recalculate path” to “Never”.

When I start my game my agents don’t move because they wait that I use my script to instantiate a wall and to update their path with SearchPath().

If I instantiate a wall, they will calculate their path and start to move, which means that SearchPath() works.
If I instantiate a wall close to their path, they will consider it and update immediately their path as expected to take the erosion into account.
However, if I instantiate a wall on their path (such as in the next picture) it looks like the path is calculated without taking the obstacle into account.

To overcome this issue I have to instantiate another wall anywhere on the grid (to use SearchPath() again) or I have to wait the next automatic update (if enabled). I checked the console and my logs show that “instantiate” happens first even when the issue occurs. Therefore, I do not understand why SearchPath() does not consider the instantiated wall in this very specific situation.

Does someone knows why this is happening?

Thank you very much for your help!

The code:

public class InstantiateObjectOnGrid : MonoBehaviour
{
private Camera mainCamera;
[SerializeField] GameObject wall;
AIPath[] aiPath;
Grid grid;

void Start()
{
    mainCamera = Camera.main;
    aiPath = FindObjectsOfType<AIPath>();
    grid = FindObjectOfType<Grid>();   
}

void Update()
{
    InstantiateGameObject();
}

public void InstantiateGameObject()
{
    if (Input.GetMouseButton(0))
    {
        Vector3 position = CameraGridPosition();
        Vector2 position2D = new Vector2(position.x, position.y);

        if (Physics2D.OverlapPoint(position2D) == false)
        {
            Instantiate(wall, position, Quaternion.identity);
            Debug.Log("Instantiate");
            UpdateAI();
        }
    }
}

public void UpdateAI()
{
    foreach (AIPath aiPath in aiPath)
    {
        aiPath.SearchPath();
       Debug.Log("AI Updated");
    }

}

public Vector3Int CameraGridPosition()
{
    Vector3 worldPosition = mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -mainCamera.transform.position.z));
    return grid.WorldToCell(worldPosition);
}

}

Hi

When you create a DynamicGridObstacle it will schedule the graph update in the next Update loop. This means that you tell the agent to recalculate its path before the DynamicGridObstacle has scheduled the graph update. If you want to force the component to schedule the graph update immediately you can do:

var wall = Instantiate(...);
wall.GetComponent<DynamicGridObstacle>().DoUpdateGraphs();

Thank you very much Aron! I followed your advice and it works.

1 Like