• 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
Question by Divinitize1 · Feb 12, 2020 at 10:46 PM · navmeshnavigationedge detection

Unity navmesh, navigate to closest point of clicked object

See the image to understand what I am trying to explain. I want to click on t$$anonymous$$s tree and for the agent to find the closest point between it and the tree and navigate there, however it seems to just pick random points around each object and walks there. The best t$$anonymous$$ng I could find w$$anonymous$$ch could possibly help is NavMesh.FindClosestEdge however I can't seem to get it to work with what I want to do with it. I tried on the Unity navigation forum and had no response in 48 hours.

alt text

 if(Input.GetMouseButtonDown(0))
         {
             Ray ray = cam.ScreenPointToRay(Input.mousePosition);
             RaycastHit rayHit;
             NavMeshHit navHit;
 
             if(Physics.Raycast(ray, out rayHit))
             {
                 float dist = Vector3.Distance(rayHit.point,transform.position);

                 //For general ground navigation
                 if(dist > .5f && rayHit.transform.gameObject.layer == LayerMask.NameToLayer("Ground"))
                 {
                     agent.SetDestination(rayHit.point);
                 }
                 //If click tree!
                 if(dist > .5f && rayHit.transform.gameObject.layer == LayerMask.NameToLayer("Tree"))
                 {
                     if (NavMesh.FindClosestEdge(rayHit.transform.position, out navHit, NavMesh.AllAreas))
                     {
                         DrawCircle(navHit.position, navHit.distance, Color.red);
                         Debug.DrawRay(navHit.position, Vector3.up, Color.red);
                         agent.SetDestination(rayHit.transform.position);
                     }
                 }
             }
         }

What i currently have above does not work, because i can only assume it's looking for an actual navmesh $$anonymous$$t rather than a gameobject $$anonymous$$t. Any help would be so greatly appreciated, i really can't understand how t$$anonymous$$s isn't a common issue with Navmesh games.

navigation.png (122.6 kB)
Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by unity_ek98vnTRplGj8Q · Feb 13, 2020 at 04:55 AM

First of all, t$$anonymous$$s line agent.SetDestination(rayHit.transform.position); sets the destination to the position of the gameobject that you clicked. T$$anonymous$$s is why your agent is going to a random side, because your requested destination is right in the middle of the gap in the navmesh. Putting in navHit.position or even rayHit.point will likely yield more consistent results. If you want your player to go to the closest point to $$anonymous$$m, then an easy way could be to just nudge your destination towards your player, maybe somet$$anonymous$$ng like

 Vector3 playerDirection = transform.position - ray$$anonymous$$t.transform.position;
 agent.SetDestination(rayHit.transform.position + playerDirection.normalized);
Comment

People who like this

0 Show 3 · 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 unity_ek98vnTRplGj8Q · Feb 13, 2020 at 04:57 AM 0
Share

Also as a note, I'm pretty sure (although I haven't used Unity's navmesh in a while), requesting a destination that is off the navmesh will automatically path you towards the closest location that is on the navmesh, so you don't have to go through the trouble of calculating it yourself

avatar image Divinitize1 unity_ek98vnTRplGj8Q · Feb 13, 2020 at 06:03 PM 0
Share

I'll try the "nudge" method later, but I have tried the regular rayHit.point and navhit.transform.position and both are exactly the same.

For now i have done a dirty hack by adding an empty gameobject around the tree which rotates towards the player with an offset to the tree, and clicking the tree will navigate to that invisible object.

avatar image Divinitize1 · Feb 13, 2020 at 07:54 PM 0
Share

I'll mark this answer as correct, because rayHit.point for even clicking on a gameobject does infact "almost" work the way i want it too. I think i spent so much time trying rayHit.transform.position assuming that I need to treat the hit as a gameobject rather than a regular hit that i never really tried just using the standard rayHit.point. Some more tweaking is needed to get it working perfectly but it's good enough for now thanks.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

126 People are following this question.

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

Related Questions

NavMeshAgent occasionally makes stale paths 0 Answers

NavMeshAgent hindered by rigidbody 2 Answers

Navmesh baking button greyed out 3 Answers

Navmesh agent clearly not finding the most efficient route... 1 Answer

NavMeshPath.corners.length is always 0 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