GetNearest() useless or I'm doing something really wrong

Hi folks,
I have a grid graph with 2 or 3 walkable areas surrounded by unwalkable terrain.

I calculate a random point in the first area and a second random point in the other side of the terrain. This means the second one could be over a different area or even over an unwalkable zone. So I ask for the nearest node constraining only the walkability to true and the area to be the same as the one under the first point.
What I expect is to get a walkable node on the same area of the first point no matter how far it may be from the second point (the one passed to GetNearest()). I made sure such point exists and the grid max nearest search distance is set to 1000 which is even bigger than the grid itself.
But what I get is simply the node under my random point ignoring the constrains. The constrained node of the NNInfo is null…

Reading the docs for NNInfo.ConstrainedNode it states:

Optional to be filled in.

If the search will be able to find the constrained node without any extra effort it can fill it in.

I really cant understand what this means. A constraint is a constraint, not a “suggestion”. Its useless like this…
Or I am doing something really wrong?

If anyone could throw light to this obscure problem…
Thx in advance.

Could you please post the code you are using?

The constraints are hard, not suggestions, so you are doing something wrong, but I cannot know for sure until I can see your code.


` int margin = 30;
int offset = 50;

	Vector3 startPos = new Vector3 (Random.Range (this.NavigableAreaStart.x + margin, this.NavigableAreaStart.x + margin + offset), 0, Random.Range (this.NavigableAreaStart.z + margin, this.NavigableAreaStart.z + margin + offset));
	Vector3 endPos = new Vector3 (Random.Range (this.NavigableAreaEnd.x - margin - offset, this.NavigableAreaEnd.x - margin), 0, Random.Range (this.NavigableAreaEnd.z - margin - offset, this.NavigableAreaEnd.z - margin));

	NNConstraint constraint = new NNConstraint ();
	constraint.constrainArea = false;
	constraint.constrainDistance = false;
	constraint.constrainTags = false;

	constraint.constrainWalkability = true;
	constraint.walkable = true;

	NNInfo startPosInfo = (startPos, constraint);

	constraint.area = (int)startPosInfo.node.Area;
	constraint.constrainArea = true;

	NNInfo endPosInfo = (endPos, constraint);

	Debug.Log (startPosInfo.constrainedNode);
	Debug.Log (endPosInfo.constrainedNode);
	this.PathStartPosition = (Vector3)startPosInfo.node.position;
	this.PathEndPosition = (Vector3)endPosInfo.node.position;

	this.PathStartPosition.y = Terrain.activeTerrain.SampleHeight (this.PathStartPosition) + 1;
	this.PathEndPosition.y = Terrain.activeTerrain.SampleHeight (this.PathEndPosition) + 1;`

Here is a screenshot too:
The “PathEnd” gameobject is set to the previous PathEndPosition. I want it to stay on the same “island” on the “Spawner” GO.
<img src=’/uploads/default/497/654bdce03c526f9c.png’

Any update on this one?

Ah, you are using the GridGraph’s GetNearest method directly. You shouldn’t do that in most cases. Instead use If you want to search only a specific graph (if you have more than one), use the graphMask field on the constraint.

The normal GetNearest method on GridGraphs is meant to be as fast as possible and thus only returns the closest node without any respect for the constraint. The GetNearestForce method has some more complicated logic to handle constraints (but as said, you shouldn’t use these methods directly in most cases).

Solved. Thx.
I think you should put some warning on the docs saying it’s not meant for public use.