How to use Obstacles2D?

Godot Version

Godot 4.2.1

Question

I am using the navigaiton system for a 2D project. I was able to do what I wanted by looking at videos on the internet. My character reaches the point I want by following the areas I specify.
But here’s the problem; When I add a new obstacle to the path of the character who will move in the game, it does not see what I add as an obstacle. I think the “NavigationObstacle2D” node is used for this.
As far as I understand, I am drawing a certain area in the obstacle object, and if it is within the area I have drawn with a “NavigationRegion2D”, the “NavigationAgent2D” object needs to go around this obstacle in order to follow the road. This is exactly what I want, but it does not see the obstacles object at all and continues to pass through it. Indeed, this obstacle system has the least information I could find about Godot on the internet. I almost couldn’t find anything explaining the system and how it works. Even after my own tinkering, the problem still persists. If there is anyone who has worked on this before, I would like them to help.

Here’s the documentation page about NavigationObstacles Using NavigationObstacles — Godot Engine (stable) documentation in English

Read the Note

NavigationObstacles do not change or influence the pathfinding in any way. NavigationObstacles only affect the avoidance velocities of agents controlled by avoidance.

Yes, I read in a few places that this is how it is used, but it is not true. I think I need to recreate the path for each obstacle every time, so are there resources on how to do this?

You might want to do this:

When you start game, navmesh looks like this and then when you do something, it becomes like this:

So your characters will fing new path

So, you need to add Polygon2D as a child of NavigationRegion2D
image

Now here in Sprite2D I have this code:

I set up timer for 0.1 seconds and then if you, in my situation, press Left click, navmesh rebaking but without this region where polygon2D is

Although, if you want to work with obstacles 2d, I recommend to watch this video.

You have to write some code with velocity computed

1 Like

Thanks, it really worked. When I added the newly added object as a child of the NavigationRegion and triggered the BakeNavigationPolygon again, everything was as I wanted. Thanks a lot.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.