I want to find the total distance of the path that an agent is following. This is so I can determine if the the calculated path is out of reach for the agent (i.e. prevent targeting through walls)
I don't see a method to do this in the documentation (distance_to_target appears to return the distance from the agent to the target, not the length of the path). So my plan was to calculate it using get_current_navigation_path, but it appears to not return a value even when a path is clearly being followed (see picture). Am I doing something wrong with navigation / are there different approaches to solve my issue?
#get_current_navigation_path not returning a path
4 messages · Page 1 of 1 (latest)
Relevant code:
extends CharacterBody2D
const SPEED = 100.0
var state = MOVEMENT_STATES.IDLE
var tracking
@onready var nav_agent = $NavigationAgent2D
enum MOVEMENT_STATES {
IDLE,
WANDER,
FOLLOW,
RUN
}
func _physics_process(delta):
if tracking != null and state != MOVEMENT_STATES.FOLLOW:
if _get_target_distance() <= $TargetDetection/CollisionShape2D.get_shape().get_radius():
state = MOVEMENT_STATES.FOLLOW
match state:
MOVEMENT_STATES.FOLLOW:
var direction = to_local(nav_agent.get_next_path_position())
$MovementController.move(direction, SPEED)
move_and_slide()
func _on_target_detection_body_entered(body):
tracking = body
nav_agent.set_target_position(tracking.global_position)
$Timer.start()
# if player is higher level than self, run
# have target range increased based on player level ?
func _on_target_detection_body_exited(body):
if body == tracking and state != MOVEMENT_STATES.FOLLOW:
tracking = null
$Timer.stop()
func _on_timer_timeout():
nav_agent.set_target_position(tracking.global_position)
print(nav_agent.get_current_navigation_path()) #<----
func _get_target_distance() -> float:
var points = nav_agent.get_current_navigation_path()
var length = 1
var distance = 0.0
while length < points.size():
distance += points[0].distance_to(points[1])
return distance
Want to prevent tracking in scenario like this, where distance from start to end is clearly larger than the detection radius
@mortal breach You could check through the tilemap if surrounding area of the entity is near a wall or the direct straight line of the entity being tracked is not a wall. You can also use raycast, I guess that could be more drain than needed and less efficient when it comes to performance.