- Home /
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.
The closest thing I have found looking around is this question. Specifically, the last answer, which seems to suggest that a collider is needed in order to do get a point on a Navmesh using Raycast. He then uses Navmesh.SamplePosition to get the actual point on the Navmesh. Every other source doesn't indicate the need for a collider in addition to the Navmesh in order to do this. Any help?
Your Deb rays actually don't look like tthe RC hits anything. The lines are still showing through the mesh whereas they should probably stop there, depending on how your set up your Debug rays of course.
I think that is only because of the way I call Debug.DrawRay. I pass the origin and the direction multiplied by an arbitrary constant (1000). Also, that is exactly the problem, the Raycast doesn't hit anything although when I draw it, i looks like it should.
The other version casts along the nav mesh. As if you were raycasting forward with your player to check four walls. Flat 2D cast on ground.
The Nav$$anonymous$$esh.Raycast API does a good job of answering your question in the description. http://docs.unity3d.com/ScriptReference/Nav$$anonymous$$esh.Raycast.html
The other version (Nav$$anonymous$$esh.Raycast) works in 2.5D and only checks for Nav$$anonymous$$esh boundaries ins$$anonymous$$d of real 3D space. The navigation system would take care of making a path for walking. But suppose you only wanted to let your character obey move commands to where they could see. You would use Nav$$anonymous$$esh.Raycast to check if they had a line of sight to their target and then you would use the navigation system to move there.
Answer by oscarlosu · Mar 17, 2015 at 12:19 AM
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
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.
Now that you posted a longer description - then yes, this is the common way to do what you're asking. The ground generally needs to have a collider, and then you raycast against that to get where you're clicking and the agent should move.
AFAI$$anonymous$$, there's no way to find "where did I click on the navmesh" without having an underlying collider. Which is kind of a shame, as moving with a navmesh means that your ground shouldn't really need a collider.
OR You can set your camera view straightly top-down. ↓ Like it's a 2D. This also fixes the problem.
Your answer
Follow this Question
Related Questions
Weird navigation issue 0 Answers
NavMesh - fatal error during baking 0 Answers
How can I find if a raycast has passed through two points? 2 Answers
Navmesh baking button greyed out 3 Answers
Navmesh agent clearly not finding the most efficient route... 1 Answer