Sample Position Replication - Recast Graph


I try to do a SamplePosition as there is unitys NavMesh component system. I tried to do it this way. My problem is that the original SamplePosition of the NavMesh works a little different, as can be seen in the api. It actually return true/false if there is a sampled position nearby. Therefore i build this function:

private static bool SamplePosition(Vector3 _destination, out NNInfo _hit, float _maximalSamplePositionDistance)
    _hit =, NNConstraint.Walkable);
    return (_hit.position - _destination).sqrMagnitude < Mathf.Pow(_maximalSamplePositionDistance, 2);

I have not tested other graph types yet but on a recast graph it always finds a node in proximity to the target destination regardless if there actually is a walkable area (a build navmesh as there would be in unity) nearby.

It probably checks the closest voxel, I assume. Because as long as the I check for a point inside the recast graph’s volume, it returns the same position as my “_destination” parameter.

Kind regards.


Your code should work perfectly well, I think.
Though I can’t recall exactly what hit.position will be when the search cannot find any close nodes. In the beta it will always be (+inf,+inf,+inf) in that case, but in 4.2.19 it might be something less consistent.
You may want to add return _hit.node != null && ... to make it more predictable in 4.2.19.

Thank you for the quick response!

And I have to apologize. Everything worked fine. I made a mistake in my architecture!

It was a long day and I got sloppy :sweat_smile:

1 Like