NavigationAgent2D ignoring the bounds of NavigationRegion2D's NavigationPolygon

Godot Version

4.2.2

Question

Good morning fellow Godot devs!

I’ve implemented pathfinding for the very first time and I have encountered an unexpected behaviour.

The PathfinderAgent2D having scene is supposed to follow the player around and avoid some obstacles that are within the tilemap. These obstacles have the physics layer drawn on it and no navigation layer. All the ‘allowed’ tiles have the opposite situation.

Afterwards I bake the NavigationPolygon within the NavigationRegion2D node in the level scene and it looks like this:

But when I run the scene, the enemy will look for a direct path to the player, avoiding the obstacles but very often choosing a path that is outside of the blue bounds of the NavigationPolygon and get way too close to the obstacle ultimately getting him stuck.

Here’s what I mean:

Good decision:
image

Bad decision:
image

On the second printscreen he is ignoring the bounds of the NavigationPolygon. He swaps often between these two kind of paths, and sometimes gets stuck like so:

image

HELPFUL INFORMATION:

Enemy code:

class_name Enemy extends Entity

@onready var navigation_agent_2d : NavigationAgent2D = $NavigationAgent2D
@onready var update_pathfinder_timer : Timer = $UpdatePathfinderTimer

var player : Player
var path_to_player : Vector2

func _ready():
	player = get_tree().get_first_node_in_group("player")

func _physics_process(delta):
	current_direction = path_to_player
	resting(delta)
	move()

func get_path_to_player() -> Vector2:
	navigation_agent_2d.target_position = player.global_position
	var next_path_position = navigation_agent_2d.get_next_path_position()
	var direction = global_position.direction_to(next_path_position)
	
	return direction
	
func _on_update_pathfinder_timer_timeout():
	path_to_player = get_path_to_player()

Here are the NavigationRegion2D inspector settings:

Here are the NavigationAgent2D inspector settings:

Any ideas on why he’s bouncing between good and path pathfinding decisions? Thanks in advance!

1 Like

Looks like you have both the navigation mesh from the NavigationRegion2D and the build-in tile navigation mesh from the TileMap active at the same time creating conflict. You can disable the TileMap navigation on the TileMapLayers.

The “bad” pathfinding is a typcial result of what you get from the TileMap build-in navigation. Each tile adds its own navigation region and navigation mesh so you have edges between each tile what is visible in image 3 and 4 with the “bad” result.

The TileMap can not add the required border margin so your agents do get stuck everywhere because the tile navigation mesh aligns with the physics collision. That does not work, as a navigation mesh is for the center of an agent while physics is for its outer shape.

To keep things functional there always needs to be at least the agent radius as a margin between navigation mesh edges and physics edges which is what the NavigationRegion2D adds when baked but the TileMap does not (and can not).

Thank you very much, removing the navigation layer from the tilemap worked. So if I got this straight, you either use the NavigationRegion2D node to bake paths for agents to travel on (and this will take in consideration collision shapes) or you use the navigation layer of a tilemap to define them but using this you must be careful and utilize the agent’s radius as margin (when you say this do you mean the settings in the avoidance tab on the Agent’s inspector?).

image

Because otherwise I don’t see anywhere I can control this on the agent. Maybe with code?

Anyways, thanks again. Everything is working fine!

The agent_radius on the NavigationAgent2D is only for avoidance.
The agent_radius that affects the navigation mesh baking is found on the NavigationMesh / NavigationPolygon resource when baked.

1 Like

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