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:
- 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!