Help my pathfiding code keeps breaking

Godot Version

4.2

Question

hello my code keeps breakin its either just standing still or going to costco and flying randomly, i use this system to make him stop for breaks but he still does nothing even when theres no timer stuff

also the one thats freezing is the one with to_local and the one flying is this current one

thanks in advance :3

extends CharacterBody3D

var speed : float = 40
@export_category("pathfindspot")
@export var point : Node3D
@export var reststop : Node3D

@onready var agent : NavigationAgent3D = $NavigationAgent3D

func _ready():
	pass


func _process(_delta):
	pass

	var dir : Vector3 = agent.get_next_path_position().normalized()

	if $go_run_boy.is_stopped() == false:
		agent.target_position = point.global_position
		velocity = dir * speed
	if $im_so_tired_boss.is_stopped() == false:
		agent.target_position = reststop.global_position
		velocity = dir * speed
	move_and_slide()

func _on_im_so_tired_boss_timeout():
	$go_run_boy.start()

func _on_go_run_boy_timeout():
	$im_so_tired_boss.start()

Why do you normalize position?

Ohhh i didnt think of that thanks for the reply it wirks now

wait on second thought its still really broken it still flies to the north east the nomalized only slowed it, sorry if this is confusing but still thanks for the help

It behaves strangely because you use the position as if it was a direction.

i tried to find other ways but i think theres none, i also used that way for my other pathfinding agents and they work, out i do not know how to fix this please help

To calculate the direction from position A to position B, subtract B from A and then normalize the result.

i tried doing that but it still doesnt work so i tried rewriting it this is the code

extends CharacterBody3D

var speed : float = 5
var target : Vector3

@onready var agent : NavigationAgent3D = $NavigationAgent3D

func random_negative_or_positive():
	var rand : int = randi_range(0,1)
	if rand == 0:
		return 1
	if rand == 1:
		return -1

func get_offset_target_location():
	var offset_x : float = randf_range(10,30) * random_negative_or_positive()
	var offset_z : float = randf_range(10,30) * random_negative_or_positive()
	return global_transform.origin + Vector3(offset_x,offset_z,0)

func _process(_delta):

	if $go_run_now_boy.is_stopped() == false:
		var navmesh =  agent.get_navigation_map()
		var safe_positions : Vector3 = NavigationServer3D.map_get_closest_point(navmesh,target)
		agent.target_position = safe_positions
		var dir = to_local(agent.get_next_path_position())
		velocity = speed * dir
		print(agent.get_next_path_position())

	move_and_slide()

func _on_timer_timeout():
	target = get_offset_target_location()
	$Timer.start()


func _on_navigation_agent_3d_target_reached():
	print("aha")

it now behaves strangely the pathfinding keeps reverting to the player start position at the first frame it also goes up somehow like flying up, i like this one better because of the system is more complex and cool but it keeps on breaking
currently this version is just standing still but before it was flying

var dir: Vector3 = (agent.get_next_path_position() - global_position).normalized()

It stops when it hits the first path other than that everything works, but the path is also always the players spawn like only thay i didnt even ahave the player as the variable

I don’t understand what you mean here.

Oh ithink i know hat i did weong, when I used the agent.get_next_path_position - global_position it reverted my path to be in the same place if i dont use the minus it’ll do weird stuff so i probably dont want to use the minus so the thing doesnt move bcs if that

Ohhhhhhh i messed up so bad,
My navigation region was way too low so it was weighing him down, i also changed it to be

var dir = global_position.direction_to (agent.get_next_path_position)

This will calculate the same vector the code I posted earlier would. Try it out.
Note that you’re missing the function parentheses after get_next_path_position

Oh yeah sorry i was writing it out by hand on my phone also thanks