Add or remove obstacles during runtime

Hi, I’m a beginner developer who is very satisfied with using this asset. My game is a 2D top-down game. Currently, at the start of the game, either obstacle positions are predetermined or obstacles are generated once during runtime, followed by AstarPath.active.Scan();.

As a new content for the game, during in-game play, I want to generate obstacles at random positions within the game map at regular intervals (every 30 seconds), and when the player destroys the generated obstacle, I want them all to be registered as path obstacles.

However, as mentioned in the documentation, scanning the entire map every 30 seconds with AstarPath.active.Scan(); seems unnecessary and slow. Is there any other good way? For example, a function that rescans only the generated object part…

I came across the function mentioned in the attached screenshot while browsing the documentation! After using a box collider 2D for obstacle objects, would it be sufficient to call the UpdateGraphs (bounds) function with the size and position of the box collider 2D as the bounds? If there’s a better way, please let me know!

Yes, that would be just what you want.

1 Like



Even when I call the function AstarPath.active.UpdateGraphs(), obstacles do not get updated… Am I missing something?

If you scan the graph using the inspector, does it detect the obstacle then?

If it does, double-check your bounding box so that it is correct. Collider bounding boxes are sometimes not actually set by Unity until the next physics update after it is created.

1 Like

After creating an obstacle during runtime, I called AstarPath.active.UpdateGraphs(bounds); to check if it’s processed correctly. I tested it using AstarPath.OnGraphsUpdated += LogMethod; and the log is called properly. Does this mean there might be something wrong with the bounds parameter used in UpdateGraphs?

Also, when removing an obstacle during runtime, should I disable the obstacle first and then call AstarPath.active.UpdateGraphs(bounds)?

I’m trying to check the official documentation, but my English is not very strong, so it’s quite challenging. Sorry!

Yes, it works correctly when I generate the graph using the inspector!

In order to operate normally
Are separate components needed?
Components belonging to the current obstacle object are
With sprite renderer
Only Box Collider 2D exists.

Use Debug.Log to check what the bounding box is, and if it seems reasonable.

Yes

As long as it is detected using the normal “Scan” button, the graph update should work fine as well.

1 Like

AstarPath.active.UpdateGraphs(objCollider.bounds); Instead of

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

Even if I use it like this, the obstacles are not updated…

  1. This is the obstacle parent object.
    It contains a box collider, but this is used to detect the player character.
    It is a box collider (yellow arrow points).
    IsTrigger is set to True and the layer is not an obstacle layer.


2. This is an obstacle object that must actually collide with physics.
It is a child object of the obstacle parent object.
It is actually set as an obstacle layer and the box collider component is
It exists (red arrow points to it)

AstarPath.active.UpdateGraphs is called from the obstacle parent object using the collider of the child object registered in the inspector.
Is there anything that could be a problem among the above?

The version currently in use is 5.0.2.
Even if you upgrade the asset version to the latest version, it doesn’t work.

Physics2D.autoSyncTransforms
Physics2D.SyncTransforms
Even if you use , it doesn’t work.

AstarPath.active.UpdateGraphs(objCollider.bounds);
after
AstarPath.active.FlushGraphUpdates(); Even if you call the function
It doesn’t work…

In the end I temporarily
AstarPath.active.Scan();
I decided to use…

Maybe I made a mistake
If there is anything I need to check clearly, please let me know.

Hi

Did you double-check what value of objCollder.bounds you are passing to the UpdateGraphs method?

1 Like

Yes, I’ve confirmed it. The size logged in the log is (2.20, 1.65, 0.00), which matches the size of the obstacle’s BoxCollider2D.

I’m not sure what’s going wrong. I have a potential solution in mind: currently, the AI (monsters) in my game are using the RVO Simulator and RVOController. I’m thinking about using the RVOController for the obstacles as well, and setting ‘canMove’ to false to see if they will then behave like static obstacles. I am planning to test this out.

If I use the RVOController component on obstacles, I wonder if it will prevent the AIs from colliding with them…?
What are your thoughts on this approach?

Did you verify the position too?

You can mark the RVOController as locked. That will make other agents try to move around them. However, keep in mind that this is not feasible for larger obstacles, as the local avoidance algorithm is not a pathfinding algorithm.

Yes, that’s right, I also checked the location…T.T

I use RVO for obstacles and it seems to work to some extent.

But, like you said
AI does not recognize obstacles as objects to be avoided…

Hmm. Could you try waiting for a frame or two before calling UpdateGraphs? It’s possible Unity does not update collision in time.
For a simple test, you could run UpdateGraphs(bounding box, 0.1f); to make it wait 0.1 seconds before applying the graph update.

1 Like

Thank you, I will try it out!