Issue with GetNearest

Hello,

I’m trying to determine if an agent can reach a target position but I’m having an issue with the AstarPath GetNearest method.
It will return a node that the agent can’t reach.
There is probably something that I don’t understand properly here.

To better illustrate my issue:
image

  • Green cross is the target position
  • Yellow cross is the node returned by GetNearest
  • Red cross is where my agent will endup (and the node I was hoping to get from GetNearest)

Here is the code I am using for this

	public bool CanReachPosition(Vector3 p_targetPosition, float p_targetDistance)
	{
		var l_userNode = AstarPath.active.GetNearest(transform.position, NNConstraint.Default);

		//!!! This returns node that is not reachable
		var l_targetNode = AstarPath.active.GetNearest(p_targetPosition, NNConstraint.Default);

		//It may happen that target is not even on graph
		if (l_targetNode.node == null)
			return false;
		//Or path is not possible
		else if (l_targetNode.node != null && l_userNode.node != null && !PathUtilities.IsPathPossible(l_targetNode.node, l_userNode.node))
			return false;
		//or the distance between target node and target position is too big
		else if (l_targetNode.node != null && l_userNode.node != null && p_targetDistance >= 0)
		{
			if (Vector3.Distance(l_targetNode.position, p_targetPosition) > p_targetDistance)
				return false; 
		}
		

		return true;
	}

Thank you for any help or explanation you could provide!

Okay after some heavy digging in the forum I got my answer.

I must use TargetNode.node.position and not TargetNode.position

1 Like

Sorry for the late reply.
Yes, NNInfo.position will return the closest point on the navmesh surface, while NNInfo.node.position is the center of the closest node.