hey I followed a tutorial on pathfinding
I’m trying to make the NPC path to the marked destination (see attached images). it perfectly works until it reaches the position → there it just jitters and never comes to a halt
do you need more info? what might be the cause of the jitter? I’m guessing it’s because _process() is called every frame?
Tis is the agent’s code:
func _physics_process(delta: float) -> void:
var direction := Vector3.ZERO
nav_agent.target_position = target[self_id].global_position
direction = nav_agent.get_next_path_position() - global_position
direction = direction.normalized()
velocity = velocity.lerp(direction * speed, accel * delta)
look_at(get_node("../StrawberryStand").global_position)
move_and_slide()
The docs recommend 2 checks, the second check should stop the jittering. They mention it somewhere in the docs.
func _physics_process(delta):
# Do not query when the map has never synchronized and is empty.
if NavigationServer3D.map_get_iteration_id(navigation_agent.get_navigation_map()) == 0:
return
if navigation_agent.is_navigation_finished():
return
Try only normalizing the direction if it’s greater than 1, so it doesn’t overshoot the target.
func _physics_process(delta: float) -> void:
var direction := Vector3.ZERO
nav_agent.target_position = target[self_id].global_position
direction = nav_agent.get_next_path_position() - global_position
if direction.length_squared() > 1.0:
direction = direction.normalized()
velocity = velocity.lerp(direction * speed, accel * delta)
look_at(get_node("../StrawberryStand").global_position)
move_and_slide()
I’d also recommend against using lerp, try using move_toward instead and set your accel to be in terms of pixels per second, a value twice or four times your speed might feel good. lerp is frame dependent, especially when multiplied by delta, where move_toward is not.