0 votes

I tried following Garbaj's basic enemy pathfinding code, but even though I'm pretty sure I've copied his code exactly my enemies aren't moving. This is my code for the enemy:

extends KinematicBody

onready var navigation = get_parent()
onready var player = $"../Player"

var health = 20
var speed = 10

#ai variables

var path = []
var path_node = 0

func _ready():


func _physics_process(delta):
    if path_node < path.size():
        var direction = (path[path_node] - global_transform.origin)
        if direction.length() < 1:
            path_node += 1
            move_and_slide(direction.normalized() * speed, Vector3.UP)
func move_to(target_pos):
    path = navigation.get_simple_path(global_transform.origin, target_pos)
    path_node = 0
    print("moving to")

func _on_UpdatePath_timeout():

the node structure in my scene goes like this:


I ran some print functions to do some testing, and while moveto() is being called correctly, the else statement which the moveand_slide() code is on is not being run at all unless the player is moving. As soon as the player stops moving the else statements stops running, but the enemy doesn't move an inch either way.

I have no idea what's going wrong, and I really appreciate any help.

Godot version 3.4.4
in Engine by (391 points)
edited by

Hey don't have much info, but

  1. check if the timer function is actually connected
  2. Is the nav mesh actually visible in editor setting

Yes the signal is connected, and I can see the navmesh in the editor. =D

1 Answer

+1 vote
Best answer

You need to print more :)
Print direction.lenght, print path node, print path size.
I would guess, that signal is somehow continously fired endlessly updating player position and resetting path_node to 0. Path is endlessly updated, so it only ever gets to path[0] which is basically enemy transform origin, so direction.lenght is 0 and no movement happens.

by (7,925 points)
selected by

Ok, I did a bunch more printing and here's what I discovered:

The function move_to() is being called every 2 seconds like it's supposed to.

Path node is always zero.

The path size is always zero.

Because of this, all the code under my if statement if path_node < path.size() is not running.

So I conclude that the path is not being generated correctly, because it should definitely be more than zero sometimes right?

So I printed the player position every time move_to() was called to make sure the player was getting accessed correctly, and the position was printed just fine.

So I guess the problem is probably in my navmesh?

Ok I feel really stupid now. The navigation mesh wasn't enabled!

Thanks for the help, you're suggestions really helped me get to the real problem. I just assumed my code was wrong, cause it's a bit complex, when really the problem was ...very simple. Anyway, thanks =D

Of course I helped :). Mostly by hinting You to print. This was the actual advice that led You to solve your problem. You can debug almost everything by strategically placing print()
Feel free to assign "best answer" if You agree ;)

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.