How to get length of the path of a NavigationAgent between point without setting it as target_position? Like - distance_to_target(target: Vector3)

Godot Version

v4.3.stable.steam

Question

To be shorter, I have NPC chatacter which:

  1. Explores the place
  2. Chases a player if finds him
  3. Continues to explore if lost or catched up the player

There will be more than one player, so the NPC will have to choose one of them, first I would like to do the simplest logic - to catch the one who is closer to him.

It means I need to get each path length to the players. Is there a way to get length to each target without changing NavigationAgent’s target? Because NPC is moving each frame by this path - and if I change it each time just to check - NPC will change his path for a second and it will look weird

Now I decided to create second NavigationAgent just for finding the shortest path (In screenshot “DestinationAgent3D” node). Maybe there’re better solutins?

Something like - distance_to_target(target: Vector3) -> float

Instead of using a NavigationAgent node you could just query a navigation path with the NavigationServer3D.map_get_path() instead if the query is simple or use the PathQuery objects for more complex queries. Using NavigationPathQueryObjects — Godot Engine (latest) documentation in English

2 Likes

Oh, I thought that there would be something better for my purpose but couldn’t find it, thanks!)
I’ll try it later :grin:

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