Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by oscarlosu · Mar 15, 2015 at 07:04 PM · raycastnavmeshnavigation

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:

alt text

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.

untitled.png (154.9 kB)
Comment
Add comment · Show 15
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oscarlosu · Mar 16, 2015 at 01:32 PM 0
Share

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?

avatar image Cherno · Mar 16, 2015 at 01:34 PM 0
Share

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.

avatar image oscarlosu · Mar 16, 2015 at 01:42 PM 0
Share

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.

avatar image hexagonius · Mar 16, 2015 at 09:39 PM 1
Share

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.

avatar image maccabbe · Mar 16, 2015 at 11:48 PM 1
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Baste · Mar 17, 2015 at 09:17 AM 1
Share

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.

avatar image theodder1 · Oct 15, 2019 at 07:01 AM 0
Share

OR You can set your camera view straightly top-down. ↓ Like it's a 2D. This also fixes the problem.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges