I have setup a character to use NavigationAgent2D for navigation which works as expected with a NavigationRegion2D. Without changing anything to the character, it does not work with a TileMap. I have added a navigation layer to a tile and can see the "visible navigation" debug draw display the navigable area correctly. The character will not move on this navigation layer though. The navigation layer ' 1 ' is enabled for both the TileMap and NavigationAgent2D. What could be wrong?
#Making NavigationAgent2D work with TileMap navigation
5 messages · Page 1 of 1 (latest)
Character code:
extends CharacterBody2D
@export var speed = 400
@onready var map : TileMap = %TileMap;
@onready var nav_agent : NavigationAgent2D = $NavigationAgent2D;
func _ready():
nav_agent.set_navigation_map(map);
func _input(event):
if event.is_action_pressed("click"):
nav_agent.target_position = get_global_mouse_position();
func _physics_process(delta):
if(nav_agent.is_navigation_finished()):
return;
var next_location = nav_agent.get_next_path_position();
var direction = global_position.direction_to(next_location);
velocity = direction * speed;
move_and_slide();
Navigation debug draw:
if only the TileMap is the difference you likely do not use the first TileMapLayer for your navigation. Every extra TileMapLayer places the navigation meshes on its own navigation map because 2D navigation meshes can not be overlapped.
You are correct - thanks for pointing that out. Can I find this information in the documentation as well?