Enemy wont move towards player Godot 4

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

So i’ve been following this tutorial on youtube on how to make the enemy ai move towards the player, i have written the bare minimum code so that the enemy ai could move towards the player but for some reason the enemy won’t move towards the player. i tried fixing it but i could not do much since i have a hard time trying to understand how godot 4 works. The code i was using for the enemy script:

@onready var nav = get_tree().get_nodes_in_group("NavMesh")[0]
@onready var player = get_tree().get_nodes_in_group("Player")[0]
@onready var nav_agent = $NavigationAgent3D

var speed = 3.0
var health = 20
var move = true

func _physics_process(delta):
	var current_location = global_transform.origin
	var next_location = nav_agent.get_next_path_position()
	var new_velocity = (next_location - current_location).normalized().z + speed
	
	var velocity = new_velocity
	move_and_slide()

func update_target_location(target_location):
	nav_agent.set_target_position(target_location)

The code for the world script:

func _physics_process(delta):
	get_tree().call_group("Enemy", "update_target_location", player.global_transform.origin)

the youtube video i was following (if it helps):
Godot 4 3D - AI Pathfinding/Navigation

Two things:
Why do you only use the z axis of direction for velocity? You are working with global coordinates, it’s possible the direction’s z component would be zero.
Second, this warning is from the godot docs:

Resetting the path every frame (by accident) might get the actor to stutter or spin around in place.

Using NavigationAgents — Godot Engine (stable) documentation in English

zhyrin | 2023-03-22 09:46