Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 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 this 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 thing I could find which 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 hit rather than a gameobject hit. Any help would be so greatly appreciated, i really can't understand how this isn't a common issue with Navmesh games.

navigation.png (122.6 kB)
Comment
Add comment
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
0
Best Answer

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

First of all, this line agent.SetDestination(rayHit.transform.position); sets the destination to the position of the gameobject that you clicked. This 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 him, then an easy way could be to just nudge your destination towards your player, maybe something like

 Vector3 playerDirection = transform.position - rayhit.transform.position;
 agent.SetDestination(rayHit.transform.position + playerDirection.normalized);
Comment
Add comment · 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 assu$$anonymous$$g 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.

Follow this Question

Answers Answers and Comments

127 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 avatar image

Related Questions

NavMesh Components for 2017.4.9f1 1 Answer

Ball flying around without reason 0 Answers

NavMesh Obstacles carve to area other than non-walkable? 0 Answers

How to make object to avoid other objects? 2 Answers

NavMeshAgent not fully reaching destination, thus it won't delete itself when it gets there 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