Problem with Navigation2D get_simple_path

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By _ul1s3s

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 move_and_slide. 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[1] - 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
add_child(line)

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.

Visible Navigation

I’m not sure about the problem but I experienced that when using tilemap. I think my navigation shape was not perfect and thus the path could not reach the desired point.

What if you try to use a NavigationPolygon you draw on top of your tilemap? That way, you’re not relying on the navigation shape of your tile.

MrEliptik | 2021-03-20 21:39