I have both colliders and tags in my game, for example the water is set as an untraversable tag for my units. I think this is related to the Constraints parameter, but I cannot figure out how to properly set it up.
But for some reason, this code can return a position that is in the water:
I find a random position inside a radius, then try to get the nearest walkable node, where path is possible, using these methods:
public static GraphNode GetNearestWalkableNode(Vector3 origin)
{
return AstarPath.active.GetNearest(origin, NNConstraint.Default).node;
}
public static bool IsPathPossible(Vector3 origin, Vector3 destination)
{
GraphNode destinationNode = AstarPath.active.GetNearest(destination).node;
if (!destinationNode.Walkable)
return false;
GraphNode originNode = AstarPath.active.GetNearest(origin).node;
return PathUtilities.IsPathPossible(originNode, destinationNode);
}
Should this code not respect tags set to untraversable? Do I need to add something more?
The full code I use to set destination is below, might be useful:
private bool TrySetTarget()
{
int retries = 0;
destination = Utils_GPS.GenerateRandomPoint(ai.originUsedForWander, Random.Range(minDistance, maxDistance));
destination = NavigationManager.GetNearestWalkablePosition(destination);
while (!NavigationManager.IsPathPossible(transform.position, destination) && retries < 100)
{
destination = Utils_GPS.GenerateRandomPoint(ai.originUsedForWander, Random.Range(minDistance, maxDistance));
destination = NavigationManager.GetNearestWalkablePosition(destination);
retries++;
}
ai.SetDestination(destination, null);
return true;
}