Hi, I'm building a top down game, I have tried to create an enemy that follows the player using Navigation2D on a Tilemap. My implementation works but just in a small area and not the whole map.
The enemy is a KinematicBody2D, I move it with moveandslide. It keeps a reference to the nav2d and the player's KinematicBody and do the following on each loop:
func get_path_to_player() -> PoolVector2Array:
return nav_2d.get_simple_path(position, player.position)
func get_moving_dir() -> Vector2:
var path := get_path_to_player()
return (path - position).normalized()
func accelerate() -> void:
velocity = get_moving_dir() * speed
This works but just in a restricted area and not the whole map. The enemy gets stuck around (272, 224).
Also, if I get the path to a specific point on the map, this happens:
var path = nav_2d.get_simple_path(Vector2(200, 200), Vector2(600,800))
print(path) # prints [(200, 192), (208, 192), (256, 208), (272, 224)]
var line = Line2D.new()
line.points = path
Enemy and line stuck
(The player is on the right. Note that even the line doesn't reach the end position, it gets to the same position when the enemy gets stuck)
Also, the setup of the tileset is correct, I put a Navigation rectangle on the whole tile that is navigable.