pathfinding script moves player instead of enemy

hello! my godot version is:

Godot v4.2.2 stable official

New forum user here, i require help with a script of mine. i did follow 2 seperate tutorials, but both gave the same result; moving the player (which is meant to be the target to move towards), instead of the enemy. (this script is on the enemies root node.

#####################################################

@onready var nav = $NavigationAgent3D
var player = null

@export var Player_path : NodePath

func _ready():
	player = get_node(Player_path)

func _process(delta):
	velocity = Vector3.ZERO
	nav.set_target_position(player.global_transform.origin)
	var nextNavPoint = nav.get_next_path_position()
	velocity = (nextNavPoint - global_transform.origin).normalized() * PSPEED
	move_and_slide()

func _physics_process(delta):
	
	if not is_on_floor():
		velocity.y -= gravity * delta
		
	move_and_slide()

#####################################################

You’ve made your code needlessly complicated. Try exporting the Player as directly instead of using the path to the player as a var. This allows you to do it like its shown below. Also the issue your having has to do with 3 things.

You’re calling move_and_slide() in both _process and _physics_process, which can cause unpredictable movement.

It seems velocity and PSPEED are not properly defined in the provided code.

Physics movement should only happen in _physics_process.

Try this instead.

extends CharacterBody3D

@onready var nav = $NavigationAgent3D
@export var player: CharacterBody3D or even Better Player (Make the player a class.)
@export var PSPEED = 5.0
@export var gravity = 9.8

func _physics_process(delta):
    # Handle gravity
    if not is_on_floor():
        velocity.y -= gravity * delta
    else:
        velocity.y = 0
    
    # Navigate toward player
    if player:
        nav.target_position = player.global_position
        var next_path_position = nav.get_next_path_position()
        
        # Calculate horizontal velocity (x and z)
        var direction = (next_path_position - global_position).normalized()
        velocity.x = direction.x * PSPEED
        velocity.z = direction.z * PSPEED
    else:
        print("ERROR: Player reference not set")
    
    # Apply movement
    move_and_slide()

To make your player a class, Give it a class name in your player.gd script

extends CharacterBody3D
class_name Player

Then you can reference other places in code by just calling its class name.

when i try this, it has the same result. the player being sucked towards to enemy like it was a black hole. I appreciate the help! however I’m sorry, i require further assistance.

@onready var nav = $NavigationAgent3D

@export var player: Player

@export var PSPEED = 5.0

var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")

func _physics_process(delta):
	
	if not is_on_floor():
		velocity.y -= gravity * delta
	else:
		velocity.y = 0
		
	if player:
                
		nav.target_position = player.global_position
		var nextpath_pos = nav.get_next_path_position()

		var direction = (nextpath_pos - global_position).normalized()
		velocity.x = direction.x * PSPEED
		velocity.z = direction.z * PSPEED
	else:
		print("ERROR: PLAYER REFERNECE NOT SET")
		
	
	move_and_slide()

The pathfinding is still not working. its like the player is pathfinding towards the enemy instead of the other way around.

Check 2 things for me really quick.

1 Script location. Double-check that this script is attached to the enemy character, not the player character

2 The scene hierarchy. Make sure the NavigationAgent3D is actually a child of the enemy node

Could you also send me a picture of your scene tree?

And also Highlight your player and enemy codes attached script and send them too like this.

enemy attached script: (this is the script with the pathfinding)
image

image

player attached script

all in all im very confused

enemy scene scene tree