NavigationObstacle2D in Godot 3.5

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By didj

Hi, I am trying to use NavigationObstacle2D to avoid moving obstacles, but I can’t make it work.

I am using Navigation2DServer and a NavigationAgent2D like so:

path = Navigation2DServer.map_get_path(
		navigation_agent.get_navigation_map(),
		global_position,
		target_position,
		true
	)

This works perfectly with the static obstacles (using a TileMap), but it does not take into account my moving obstacle, which has a child NavigationObstacle2D.

I have tried several things:

  • Manually specifying the radius of the obstacle
  • Using the following fix in the _ready function of my obstacles: Navigation2DServer.agent_set_map(nav_obstacle.get_rid(), get_world_2d().get_navigation_map())
  • ticking “Avoidance enabled” in the NavigationAgent2D
  • putting everything under a Navigation2D node

None of this worked.
Does anyone have a clue or an example to provide?

:bust_in_silhouette: Reply From: smix8

Avoidance works with velocities, not with pathfinding or paths.

In order to make it work you need something that can send a velocity to the NavigationServer for the avoidance calculation. If you use a NavigationAgent2D you can send your wanted velocity with set_velocity() and connect to the velocity_computed signal to receive the returned “safe” velocity from the server.