NavMesh.FindClosestEdge

Hi all,

I’m trying to use NavMesh.FindClosestEdge but it seems i’m not doing it right.
I’ll explain you what I want to do : I’ve got a mesh with it’s verts and tris. I’m using a Physics.Raycast to locate a new point in contact with the mesh. The I would like to find the point of the mesh that is the nearest to this new point.

I don’t really understand the role of the 3rd argument in the fonctions, Maybe the solution is here, but when I change it, there’s no difference.
Here is the part of my code that should do this, but doesn’t.

using UnityEngine;
using System.Collections;

public class Mesure3 : MonoBehaviour {
	
	public Camera mainCamera ;
	public GameObject boule ;
	private GameObject Click ;
	private GameObject Click2 ;
	
	public NavMesh mesh ;
	private NavMeshHit nearesthit ;

	void Update ()
	{
		if ( Input.GetButtonDown ("Fire1") )
		{
			if ( Input.mousePosition.x>120  Input.mousePosition.x<1160  Input.mousePosition.y>120  Input.mousePosition.y<600 )
			{
				CreatePoint ();
			}
		}
	}
	
	void CreatePoint ()
	{
		Ray ray = mainCamera.ScreenPointToRay ( Input.mousePosition ) ;
		RaycastHit hit ;
		
		if (Physics.Raycast (ray, out hit, 20.0f) )
		{
			//Create a point where the raycast intersetcs the mesh.
			Click = Instantiate (boule, hit.point , Quaternion.identity) as GameObject ;
			
			//Search the nearest point
			if (NavMesh.FindClosestEdge(hit.point, out nearesthit, 0))
			{
				//Create another point on the nearest edge.
				Click2 = Instantiate (boule, nearesthit.position , Quaternion.identity) as GameObject ;
			}
		}
	}
}

Thanks in advance

The third argument in the FindClosestEdge is a mask that lets you adjust what agents can walk on certain areas using layers. If you use 0 then it will always fail. Use agent.walkableMask instead.

Karl

I’ve tried using -1 like it’s done in the documentation, it doesn’t work too. Then I tried with 1 and same result.
The agent.walkableMask has to be used as the third argument of NavMesh.FindClosestEdge or it has to be put on the mesh on whichI want the fonction to navigate ?

It should be the agent you are testing for. A quick hack that may work would be to use int.MaxValue instead of 1. This should cause all tests to pass as every bit is 1.

Karl