Pathfinding navAgent2D next_path_position() returns position of target_position instead of next path position

Godot Version

v4.6.2.stable.official [71f334935]

Question

I’m working on 2D NPC navigation to walk to player. The attached screenshot is from a debugging session with navigation and collisions shown, with placeholder NPC (“CH”) and player (“PL”) textures. Please ignore the name of the debugging NPC.

The red line is a line-of-sight (raycast) to determine if the NPC can currently see the player at any given time. The path shown by the black dots is the NPC’s navigation agent’s path. I have the following line in the NPC’s _physics_process

localSeekPosition = to_local(navAgent.get_next_path_position())

localSeekPosition is a variable declared at the top of the NPC class as a Vector2:

var localSeekPosition:Vector2

As you can see in the screenshot, the path clearly goes up and to the right to avoid the desk. But the NPC seems to just be going straight down. Using the debugging evaluator with an inserted breakpoint, evaluating localSeekPosition returns:

x=16.503 y=44.585

Clearly, the y is positive, so, in the coordinate system, the nav agent actually wants to go down, not up. Evaluating to_local(navAgent.get_next_path_position()) also returns the same value, so there isn’t a problem in _physics

Why does get_next_path_position() not return the actual path’s next position?

Because you’re doing it backwards.

The Enemy needs a NavAgent2D node and should be making its own calls to find the path to the player. The player shouldn’t be calling to it like a game of Marco Polo.

Reverse the logic and it should work fine.

The nav agent node is a child of the NPC.

The player isn’t and has never been the one calling to it. The NPC is the one that is calling to it. This is one of the @onready statements of the NPC code:

@onready var navAgent:NavigationAgent2D = $“NavigationAgent2D”

Ok, then the code you posted is very unclear. It would help to post the scene Tree of the two objects, and the code for each.

DebugNPC

|-AnimatedSprite2D

|-CollisionShape2D

|-NavigationUpdateTimer (timer)

|-WanderingUpdateTimer (timer)

|-NameLabel (RichTextLabel)

\-PlayerCheckSightline (RayCast2D)

PlayerBody

|-AnimatedSprite2D

|-Camera2D

| |-CanvasLayer

| |-PlayerOverlay (Control)

|-PlayerCollider (CollisionShape2D)

\-PointLight2D

Please post a screen shot. A text representation is not nearly as informative as you think it is.


(Looks like the text representation also had a bunch of elements missing, sorry)

Ok, try turning on the Avoidance section of the NavigationAgent2D and play around with that.

I enabled avoidance and things seem to be much better. I’ll do some testing and I’ll mark this as solution if everything works.