Navigation Agent Pathing Issue

Godot Version

4.2.2

Question

I have these enemy nodes that are trying to squeeze diagonally between a wall tile and a barrel tile, both of which have a null navigation polygon.

This script is what my tilemap uses. Map layer 1 is for walls and map layer 2 is for objects. The barrel is an object:

extends TileMap

func _use_tile_data_runtime_update(_layer, coords):
	if coords in get_used_cells_by_id(1) or coords in get_used_cells_by_id(2): return true
	return false

func _tile_data_runtime_update(_layer, _coords, tile_data):
	tile_data.set_navigation_polygon(0, null)

My movement func in my script for the enemies:

func move():
	await get_tree().physics_frame

	if not attacking:
		if target:
			nav_agent.target_position = target.global_position
			change_facing(target.global_position)
		else:
			nav_agent.target_position = home_pos
			change_facing(home_pos)
			
		var current_agent_pos = global_position
		var next_path_pos = nav_agent.get_next_path_position()
		
		var new_velocity = current_agent_pos.direction_to(next_path_pos) * move_speed
		
		nav_agent.set_velocity(new_velocity)
		
		move_and_slide()

That is normal with such a navigation mesh layout because a navigation mesh defines the surface for the center of an agent. Navigation mesh works differently than physics collision that defines shapes.

Your navigation mesh has no agent_radius margin so the agents will move through the connected edge corners.

The TileMap build-in navigation can not create a workable navigation mesh with agent radius offset. Use a NavigationRegion2D instead to bake your TileMap and disable the TileMap build-in navigation.

See navigation mesh documentation here Using navigation meshes — Godot Engine (stable) documentation in English