NavigationAgent2D generating strange inefficient path

Godot Version

v4.3.stable.official [77dcf97d8]

Question

I am trying to get pathfinding working on a tile-based game. Right now I have two TileMapLayers, one for the floor, and one for the walls. The wall tiles are physics tiles that character bodies collide with. These TileMapLayers are then connected to a NavigationRegion2D.

Currently I am just trying to get my NavigationAgent2D to pathfind to a node that has a static, unchanging position. It seems to handle wall avoidance just fine, but once it gets near the target node the path goes off the rails and takes a very inefficient route.

My first thought was that it had something to do with the agent’s target desired distance, but modifying that hasn’t made a difference. I’m not quite sure what the agent is trying to do here.

Here is a screenshot of the path it takes (one example, other patterns of walls lead to similar results):

Here is my relevant code for the NavigationAgent2D’s parent CharacterBody2D:

func _ready():
	call_deferred("pathfinding_setup")

func _physics_process(_delta):
	var direction := navigation_agent.get_next_path_position() - global_position
	direction = direction.normalized()
	velocity = direction * speed
	move_and_slide()

func pathfinding_setup() -> void:
	await get_tree().physics_frame
	navigation_agent.target_position = target.global_position

You have set the path postprocessing to edge-centered.

This is the entire reason for this path as that snaps each path point to the middle of each crossed edge in the path corridor.

Ah, this is correct, I had forgot about this behavior.

I suppose my question would then be, is there a way to prevent the mesh from generating paths that cause this? Ideally I would like to retain path points being centered on tiles, as when I set postprocessing to corridorfunnel it causes the agent to get uncomfortably close to the walls and can lead to it getting stuck. All I can think of is just placing more walls to add more points to the mesh.

The navigation mesh baking has an agent radius settings that controls the offset.

Looks like increasing the agent radius enough to force it to path closer to the middle of floor tiles is as close as I can get it. Not sure if the most elegant solution but seems to be working well enough. Thanks!

I dont understand why you are trying to force the path in the middle when using a navmesh. The point of a navmesh is that the entire surface is usable by the agent and is as efficiently partitioned with as few polygons as possible.

If you need grid forced cells the AStarGrid2D would be a far better option.

Sorry, maybe I worded that poorly, not trying to get it exactly middle, just trying to make sure it doesn’t ride walls as much and get stuck on things. But I will look into AStarGrid2D, thank you.

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