Enemy isn't moving even though I have pathfinding code

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

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():

	pass
func _physics_process(delta):
	if path_node < path.size():
		var direction = (path[path_node] - global_transform.origin)
		if direction.length() < 1:
			path_node += 1
		else:
			move_and_slide(direction.normalized() * speed, Vector3.UP)
			print("moving")		
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():
	move_to(player.global_transform.origin)

the node structure in my scene goes like this:

Spatial
->Navigation
->->NavigationMeshInstance
->->->CSGCombiner
->->->->ABunchOfCSGMeshes
->->Player
->->Enemy

I ran some print functions to do some testing, and while move_to() is being called correctly, the else statement which the move_and_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.

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

umma | 2022-05-26 00:34

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

Millard | 2022-05-26 16:39

:bust_in_silhouette: Reply From: Inces

You need to print more :slight_smile:
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.

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?

Millard | 2022-05-27 18:58

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

Millard | 2022-05-27 19:04

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 :wink:

Inces | 2022-05-27 20:51