0 votes

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[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

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

Godot version 3.2.3
in Engine by (12 points)

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.

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.