Inconsistent NextPathPosition in godot 4.6

Godot Version

Godot Engine v4.6.dev1.mono.official.8d8041bd4

Question

Hey all,

I’ve got my navigation almost exactly how I want with the bigger issues being resolved with help from @smix8. However one of the few remaining issues is that about 1 in 3 enemy NavAgents do not path to my player NavAgent. They will instead get stuck on either the x or y axis, while going to the correct value on the other axis.

Eg. Player is at 100,100. Enemies 1 and 2 path through obstacles from 1000,1000 correctly and get to 100,100. Enemy 3 will have TargetPosition of 100,100 but its NextPathPosition() returns 100,1000 or 1000,100. It will then call NavigationFinished on reaching that position.

I assume there is some setting causing this, as my implementation seems to have correct logic even when stepping through very closely and using debug to display paths, navregions, and even my context steering values. I’ve tried turning off my context steering calls in _PhysicsProcess of my NavAgent and the issue still happens.
example output of path variables when testing:

Target Position: (29.05717, 2876.8643) <- Player Agent Position
Next Path Position: (29.05664, 4240) <- 4240 is the Enemy Agent's Y axis Position
Final Path Position: (29.05664, 4240)

NavAgent settings:

//Code when adding a new region shows it's the correct layer. Plus other units navigate fine.
NavigationServer2D.RegionSetNavigationLayers(nextNavigationRegionRid, 1);

NavAgent._PhysicsProcess:

public override void _PhysicsProcess(double delta)
	{
		if (IsNavigationFinished()) return;
		MovementDelta = (float)delta;
		
		SetInterest();
		SetDanger();
		MovementDirection = ChooseDirection();
		
        //CalculatedMoveSpeed is 100
		var calculatedVelocity = MovementDirection * CalculatedMovementSpeed;
		Parent.Velocity = Parent.Velocity.Lerp(calculatedVelocity, SteerForce);
		_ = Parent.MoveAndCollide(Parent.Velocity * MovementDelta);
	}

Set Interest function that calls NextPathPosition:

	private void SetInterest()
	{
		var nextPathLocation = GetNextPathPosition();
		var direction = Parent.GlobalPosition.DirectionTo(nextPathLocation);
		for (var i = 0; i < RayDirections.Length; i++)
		{
			var directionValue = RayDirections[i].Dot(direction);
			Interest[i] = Mathf.Max(0, directionValue);
		}
	}

Navigation Action + Responses in parent _ready:

		navAgent.NavigationFinished += () =>
		{
			Velocity = Vector2.Zero;
		};
		navAgent.TargetReached += () =>
		{
			Velocity = Vector2.Zero;
		};

The target position is emitted from another node and set in the navigation class:

//targetLocation is player agent's global position.
TargetPosition = targetLocation;

Replacing the context steering calls with a simple implementation still gives the issue.
Eg.

			var nextposition =  GetNextPathPosition();
			var newVelocity = Parent.GlobalPosition.DirectionTo(nextposition) * CalculatedMovementSpeed;
			Parent.Velocity = newVelocity;
			_ = Parent.MoveAndCollide(Parent.Velocity * MovementDelta);

Images showing what’s happening:

  • The radiating lines are the interest values from my context steering. Not really relevant to this issue IMO.
  • First image is player agent going around obstacles correctly with a normal path.
  • Second image is enemy agent correctly navigating obstacles to get to player agent.
  • Third image is enemy agent going to the correct x axis but getting stuck on 4240 y axis (its current y axis).
  • The second and third image can happen at the same time, which is part of the reason it is giving me so much trouble. I would think if there was an issue with desired distance or other NavAgent settings it would happen each time.



You can see in this image that the TargetPosition is correct but NextPathPosition is stuck on the 4240 Y value.

If anyone knows why this 3rd image situation happens I would appreciate it. I’ve been stuck on it for a long time.

Thanks for your time,
Mike

I think i’ve fixed it. It isn’t happening now that I turned on EdgeConnections for each nav region. Even though they look pixel perfect I guess there was some issue with connection the nav regions at the bottom and right side.