I made simple character movement using NavigationAgent2D and it moves on NavRegion perfectly. But now I need to add walls that may be spawned on any place on the level and Nav should build path taking that wall into account. Walls may be destroyed by damage or timer, may be built new walls on the level and so on but NavRegion is unchangable.
So I don't understand how to make NavAgent to make path around those walls. As far as I know using NavigationObstacle is not a good idea since that walls are static. So how to implement wall avoidance?
#Implementing dynamic obstacles for NavigationAgent
1 messages · Page 1 of 1 (latest)
I used navigation_agent_2d.set_velocity(new_velocity) and got signal from NavAgent
func _on_navigation_agent_2d_velocity_computed(safe_velocity):
velocity = safe_velocity
move_and_slide()
and I don't understand why character still runs into walls
Hint for NavObstacle says that NavPolygon should be simply refreshed , as far as I understand it can be refreshed ingame when something happened like wall spawned, but how can I do it?
You get the NavigationPolygon resource of your NavigationRegion2D, duplicate it, keep the original version as a backup, now change the duplicate in script for your dynamic stuff e.g. like a placed wall by removing that section of the polygon, than you replace the current NavigationPolygon on the NavigationRegion2D with the new one.
The NavigationObstacles are only for avoidance and the avoidance velocities, they wouldn't change anything about your pathfinding. If you do not cut "holes" in your polygons for you obstructions like walls the paths will lead agents still through the sections that you do not want them to use. You see that in your video, the path points still go through that "obstacle" because the navigation polygon is still below.
you can use the helper functions on the Geometry2D class to cut holes in polygons.
The problem is I cannot just make different NavPolygons cause walls may be spawned anywhere (a spell). I found somewhere it's possible to dynamically refresh NavPolygon but it's resource costly so not a good way to use while it game since it may cause freezes.
Is there really no ways to add obstacles that NavigationAgent should avoid building pathway?
pathfinding uses navigation meshes, you need to change the navigation mesh if you want pathfinding to change. No way around that.
pr https://github.com/godotengine/godot/pull/70724 will add a 2D navigation mesh bake functionality at some point but until then you need to work around that with scripts. The easist way is usually to have a NavigationRegion2D with a large navigation polygon that covers your gameplay area (or at least a large section of it in case your game is huuuge). You keep this navigation polygon with a copy as backup and when you place dynamic gameplay objects that should change it you use Geometry2D functions to cut holes in it.
Complex games like e.g. StarCraft2 did their entire navigation system like that. They kept a copy of the levels entire navigation mesh that only included the static objects. Everytime they added a dynamic object like e.g. a building they loaded the original navigation mesh, cut the holes for all dynamic objects on the map, and used that.
Holy heck but isn't it quite low performant? I didn't imagine even SC2 just regenerates nav mesh every time something is built. Well, there are just 4 vertices per building but anyway.
Thanks for an answer, I'll think about this way and will wait for final commit for that feature.