NavMesh.Raycast and Physics.Raycast never hit NavMesh

I am trying to get a NavMeshAgent to move to the position clicked by the mouse. Following the example provided in the documentation, I implemented this test class to control the agent:

public class PlayerController : MonoBehaviour {

	public float clickMaxDist = 1000.0f;
	public NavMeshAgent Agent;

	void Update ()
	{
		// Click to move agent
		if(Input.GetMouseButtonDown(0)) {
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;
			Debug.DrawRay(ray.origin, ray.direction * 1000, Color.red, 100);
			if(Physics.Raycast(ray, out hit, clickMaxDist)) {
				Agent.SetDestination(hit.point);
			}
		}
	}

}

I must be missing something, because my rays only hit when I click right on top of the cylinder, which of course, isn’t very useful to move the agent around the map.

To test if I was casting the Rays correctly, I used Debug.DrawRay:

Which, to me, looks like everything should be working.

I’ve looked around this forum and googled for a while, but everyone seems to suggest similar methods for doing this. I don’t suppose I need to add a MeshCollider to my terrain mesh for this to work, or do I?

I also tried using NavMesh.Raycast in a similar fashion, also with no results.

Anyway, I am simply puzzled by this. If anyone could point me in the right direction, it would probably spare me a lot of time.

Thank you in advance.

Hmm… I’ve done some testing and it seems you are right, NavMesh.Raycast returns false if the Raycast reaches the target and true if it hits some part of the specified NavMesh area, so it’s definitely not what I wanted. I’ll attach the test code and a demo image, in case anyone else is confused in the same way I was:

void Update ()
{
      // Click to move agent
      if(Input.GetMouseButtonDown(0)) {
	NavMeshHit hit;
	Vector3 dir = (target.transform.position -transform.position).normalized;

	bool blocked = NavMesh.Raycast(transform.position, target.transform.position, out hit, areaMask);
	Debug.DrawRay(transform.position, dir * clickMaxDist, blocked ? Color.red : Color.green, 100);
	}
}

The points in the green rays are “visible” to the cylinder, whereas the red ray shows that the ray was blocked by the NavMesh

alt text

After the discussion, and unless anyone suggests something better, my plan is to use Physics.Raycast on a MeshCollider for the surface mesh and then use NavMesh.SamplePosition to get an actual NavMesh point. I will post the code I end up using as soon as I can test it.

Thank you everyone for your help.

EDIT:

Here is the promised snippet:

// Click to move agent
		if(Input.GetMouseButtonDown(0)) {
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit physicsHit;
			if(Physics.Raycast(ray, out physicsHit, clickMaxDist)) {
				NavMeshHit navmeshHit;
				int walkableMask = 1 << NavMesh.GetAreaFromName("Walkable");
				if(NavMesh.SamplePosition(physicsHit.point, out navmeshHit, 1.0f, walkableMask)) {
					Agent.SetDestination(navmeshHit.position);
				}
			}
		}

It’s worth noting that in order to do this, you need to have a Collider with the shape of your terrain surface, which was my problem all along - as Baste, maccabbe and hexagonius pointed out. Thanks again.