0 votes

Hello! I'm working on a top-down shooter, and I'm trying to make an enemy that uses pathfinding to make its way to the player

I followed an old tutorial on Youtube and modified the results to fit with my game, but the size of the path table remains a consistent zero regardless of how much distance there is between the player and the enemy

var speed = 150
var nav = null setget set_nav
var path = []
var goal = Vector2()

func _ready():
    goal = get_tree().get_root().get_node("Node2D/Player").position

func set_nav(new_nav):
    new_nav = get_tree().get_root().get_node("Node2D/Navigation2D")
    nav = new_nav

func update_path():
    path = nav.get_simple_path(position, goal, false)
    if path.size() == 0:

func _physics_process(delta):
    if path.size() > 1:
        var d = position.distance_to(path[0])
        if d > 2:
            position = position.linear_interpolate(path[0], (speed * delta)/d)

Some help with this would be greatly appreciated

in Engine by (33 points)

Is goal being set properly - is the Player node lower in the scene tree hierarchy than your enemy node? Use this to see if it is, check the debugger to see the value of goal when the game stops:

func _ready():
    goal = get_tree().get_root().get_node("Node2D/Player").position

Yep, the coordinates of goal are the same as the player. And the player and enemy are both on the same level of hierarchy

Three things:

  1. Is your Navigation2D node _ready() at the time you call set_nav() on your enemy? Add print statements to both to make sure.
  2. Are you using a NavigationPolygon or a TileMap with Navigation2D node?
  3. Try hard coding a value to get_simple_path() and see if you get a path:


func update_path():
path = nav.get_simple_path(Vector2(0,0),  Vector2(15,15), false)

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.