how do i stop nodes from shaking with NavigationAgent2D

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

the code:

extends CharacterBody2D
const speed = 200

@export var player: Node2D
@export var marker: Node2D
@onready var nav_agent := $NavigationAgent2D as NavigationAgent2D

func _physics_process(_delta: float) -> void:
	var dir = to_local(nav_agent.get_next_path_position()).normalized()
	velocity = dir * speed
	move_and_slide()

func makepath() -> void:
	if Golbal.marker_finder == 0:
		nav_agent.target_position = player.global_position
	else: 
		nav_agent.target_position = marker.global_position




func _on_timer_timeout():
	makepath()



func _on_area_2d_body_entered(body):
	if body.is_in_group("player"):
		
		Golbal.marker_finder -= 1



func _on_area_2d_body_exited(body):
	if body.is_in_group("player"):
		
		Golbal.marker_finder += 1

@spaceyjase - you can always copy/paste this to an answer and then Hide the original comment… That’d actually be preferable for the board, since the question does have an answer… :slight_smile:

jgodfrey | 2023-07-07 22:45

:bust_in_silhouette: Reply From: spaceyjase

There’s nothing here to stop the agent when it reaches its destination; likely the shaking is because it’s constantly trying to move to another position that it never quite reaches, thus continues to make small changes and the cycle continues…

A way to stop this is to add something similar:

var distance_to_next_step = transform.origin.distance_to(dir)	
if distance_to_next_step < DISTANCE_THRESHOLD:
	return