# NavigationAgent2d is_target_reached is always returning false even when distance_to_target is 0

4.2.2.stable

### Question

I have a simple scene with a NavigationRegion2D and has a CharacterBody2D with a NavigationAgent2D. Everything is using default settings. I have baked a simple rectangle shape for the NavigationRegion2D. The navigation is working, and everything looks fine, but the is_target_reached function is always returning false, even when distance_to_target is returning 0.

Why is the is_target_reached not returning true when the distance_to_target is 0?

Here is my code for the CharacterBody2D.

``````extends CharacterBody2D
class_name Character

const SPEED: float = 200.0

@onready var nav: NavigationAgent2D = \$NavigationAgent2D

func _physics_process( delta ):
# Get the next point along the path
var next_point = nav.get_next_path_position()

# Calculate the squared distance to the next point
var distance_to_next_point_squared = position.distance_squared_to(next_point)

# Calculate the squared stopping distance
var max_travel_distance_squared = (SPEED * delta) * (SPEED * delta)

# If we are close to the target, clamp the velocity
if distance_to_next_point_squared < max_travel_distance_squared:
velocity = (next_point - position) / delta
else:
# Calculate the desired velocity
var direction = (next_point - position).normalized()
velocity = direction * SPEED

# Move the character
move_and_slide()

if nav.is_target_reached():
print( "Target Reached" )
else:
print( nav.distance_to_target() )

func set_target_position( pos: Vector2 ) -> void:
nav.target_position = pos

``````

Here is my simple script for setting the target position by clicking the mouse:

``````extends Camera2D

@export var character: Character

var mouse_pos_global = Vector2()

func _process( delta ):
if Input.is_action_just_pressed( "LeftMouseButton" ):
character.set_target_position( mouse_pos_global )

func _input( event ):
if event is InputEventMouse:
mouse_pos_global = get_global_mouse_position()

``````

Please test with the current Godot 4.3dev6 as there have been a few changes to this in the last beta versions that might already changed this.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.