private LevelGridNode GetNearestNode (Vector3 position, int x, int z, NNConstraint constraint) {
int index = width*z+x;
float minDist = float.PositiveInfinity;
LevelGridNode minNode = null;
for (int i=0;i<layerCount;i++) {
LevelGridNode node = nodes[index + width*depth*i];
if (node != null) {
float dist = ((Vector3)node.position - position).sqrMagnitude;
if (dist < minDist && constraint.Suitable (node)) {
minDist = dist;
minNode = node;
}
}
}
return minNode;
}