I’m having trouble getting the GetNearest function to return valid walkable nodes.
What I am trying to do is place a building on a location and force any units within the bounds move to the nearest walkable position. I first set the nodes at the location as not walkable and then try to get valid positions for any units within the area via GetNearest(). However the position returned is usually one of the unwalkable nodes in the area and quite often on the same node they are currently positioned.
I’ve verified that the nodes are unwalkable before calling GetNearest(). I’ve also tried setting the NNConstraint explicity and using NNConstraint .Default
Does the GetNearest function allow paths that start on unwalkable nodes? Is there anything else I’m missing?
Any help is much appreciated.
Here is the code I am using:
`
public void ClearArea()
{
MarkGrid(building.BoundingBox);
foreach (var node in EnumerateGridsInBounds(building.BoundingBox))
{
Debug.Log("Node under building is " + node.Walkable);
}
// returns objects where the building is to be placed
foreach (Unit unit in GetNegotiables())
{
var target = FindValidPosition(unit.Position, 1);
Debug.Log("Target walkable: " + GetNode(target).Walkable);
unit.MoveTo(target, false);
// TODO: mark target as not walkable
}
UnMarkGrid(building.BoundingBox);
}
public void MarkGrid(Bounds boundingBox)
{
var updateObject = new GraphUpdateObject(boundingBox)
{
updatePhysics = false,
modifyWalkability = true,
setWalkability = false
};
var astar = FindObjectOfType();
astar.UpdateGraphs(updateObject);
astar.FlushGraphUpdates();
}
public IEnumerable EnumerateGridsInBounds(Bounds bounds)
{
var astar = AstarPath.active.astarData.gridGraph;
return astar.GetNodesInArea(bounds);
}
public Vector3 FindValidPosition(Vector3 position)
{
var astar = AstarPath.active.astarData.gridGraph;
var nnc = new NNConstraint()
{
constrainWalkability = true,
walkable = true
};
var nodeInfo = astar.GetNearest(position, nnc);
// var nodeInfo = astar.GetNearest(position, NNConstraint.Default);
return (Vector3)nodeInfo.node.position;
}
public GraphNode GetNode(Vector3 position)
{
var astar = AstarPath.active.astarData.gridGraph;
return astar.GetNearest(position, ).node;
}
`
Here are the settings I am using on the graph: