How To Raycast against navmesh

Is it possible to do a raycast against the navmesh? The use-case is randomly spawning characters in a region, and I need to raycast against the navmesh to ensure I’m placing the character at a walkable location.

I suspect raycasting against navemesh to get a collision point is a common thing, but I’ve been unable to find an example.

I simple created a gameobject in a special layer (collide with anybody) with a meshcollider pointing to the mesh used in navmesh.

Thanks – can you explain how you you find/get the navmesh mesh?

Navmesh you must to drag manually your mesh into the Pathfinding. I simple create a other GameObject with a mesh collider and drag the same mesh to it.

Could be a misunderstood? I am not talking about RecastGraph.

Yeah, I am using RecastGraph. My understanding is this generates a navmesh, but maybe it can’t be easily accessed.

Or maybe not. This is unity3d, nothing could be difficult :stuck_out_tongue:

Try to dig into the code, the beautiful of source access. 10 seconds look into Pathfinding.RecastGraph class I found the method GetSceneMeshes. Could not be it, but I found some references to Mesh, and this, is what are you looking for.


The easiest method is to simply get the closest valid point.
// using Pathfinding; // At top of script

Vector3 p = Random.insideUnitSphere * 100;

NNInfo info = ( p, NNConstraint.Default );

if ( info.node == null ) Debug.LogError (“Could not find any nodes, try increasing A* Inspector -> Settings -> Max Nearest Node Distance”);

p = info.clampedPosition;

You can also get the actual mesh using
RecastGraph graph =;

graph.GetNodes ( delegate ( GraphNode node ) {
TriangleMeshNode tnode = node as TriangleMeshNode;
// The tnode variable contains everything you would want to know about a single triangle/node in the mesh
return true;

The actual nodes are stored in the recast graph inside a number of tiles.