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():
    set_physics_process(true)
    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
    update_path()

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


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

Some help with this would be greatly appreciated

in Engine by (25 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():
    set_physics_process(true)
    goal = get_tree().get_root().get_node("Node2D/Player").position
    breakpoint

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 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 webmaster@godotengine.org with your username.