Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Greyeeh · Nov 09, 2017 at 10:35 AM · unity5navmeshnavmeshagent

NavMesh Agent glitches on navmesh edges

I've recently started playing around with AI and Unity's built in pathfinding. So far I haven't had any bigger troubles with it but yesterday I imported a mountain model with a path going from it's base to the very top.

My AI game object consists of Rigidbody (set to kinematic), Capsule Collider and a NavMesh Agent. I generated the NavMesh and set max slope to 40 degrees and the AI navigates just fine when it follows my player or walks along designated paths (waypoints) but when I try to pick a random position on the NavMesh in AI's radius it glitches out and starts shaking after few seconds, usually when the destination is close to NavMesh's edge. I've captured two videos of it and I'm struggling to fix this since yesterday.

I've tried removing the Rigidbody completely, changing obstacle avoidance settings, lowering the maximum slope but nothing worked so far.

This is how it looks like:

Video 1

Video 2

The code I use to get random position:

Pastebin

         void ControlRandomWander()
         {
             float pointDist = Vector3.Distance(currentWanderPos, transform.position);
  
             if(pointDist < 2f || currentWanderPos == Vector3.zero)
             {
                 wanderWaitTimer += Time.deltaTime * 15;
  
                 anims.LookAround(true);
  
                 if (wanderWaitTimer >= wanderWaitTime)
                 {
                     Vector3 randPos = GetRandomPositionAroundTarget(transform.position, -wanderRadius, wanderRadius);
  
                     NavMeshPath pth = new NavMeshPath();
                     NavMesh.CalculatePath(transform.position, randPos, agent.areaMask, pth);
                     float pathDist = 0f;
  
                     if (pth.status == NavMeshPathStatus.PathComplete)
                     {
                         for (int i = 0; i < pth.corners.Length - 1; i++)
                         {
                             pathDist += Vector3.Distance(pth.corners[i], pth.corners[i + 1]);
                         }
  
                         Debug.Log(pathDist);
  
                         if (pathDist <= wanderRadius)
                         {
                             currentWanderPos = randPos;
  
                             wanderWaitTime = Random.Range(wanderWaitMin, wanderWaitMax);
  
                             anims.LookAround(false);
                             wanderWaitTimer = 0;
  
                             MoveToPosition(randPos, true);
                         }
                     }
                 }
             }
             else
             {
                 if (agent.destination != currentWanderPos)
                 {
                     MoveToPosition(currentWanderPos, true);
                 }
             }
         }
  
         Vector3 GetRandomPositionAroundTarget(Vector3 pos, float minRange, float maxRange)
         {
             float offsetX = Random.Range(minRange, maxRange);
             float offsetZ = Random.Range(minRange, maxRange);
  
             Vector3 orgPos = pos;
  
             orgPos.x += offsetX;
             orgPos.z += offsetZ;
  
             NavMeshHit hit;
             if(NavMesh.SamplePosition(orgPos, out hit, 5f, agent.areaMask))
             {
                 Debug.Log(hit.position);
                 return hit.position;
             }
  
             Debug.Log(hit.position);
  
             return pos;
         }

I would really appreciate any help sorting out this issue.

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

2 Replies

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

Answer by Greyeeh · Nov 09, 2017 at 12:06 PM

Turns out NavMesh.SamplePosition doesn't take NavMesh Agent radius into account so the agent can't ever reach it.

I managed to fix it by moving the hit.position by half of agent's radius away from the edge.

 if(NavMesh.SamplePosition(orgPos, out hit, 5f, agent.areaMask))
 {
      Vector3 ret = hit.position;
      Vector3 pathDir = pos - ret;
      ret += pathDir.normalized * (agent.radius / 2);
 
      return ret;
 }
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 Daemonhahn · Nov 09, 2017 at 12:17 PM 0
Share

Again the proper fix for this was contained in the threads I posted, you should be warping the navmesh agent as the current positions are desynced due to a bug.

What your doing will cause errors as sample position is currently bugged and 20% of the time will produce an erroneous result no matter whether you factor in radius or not. check the second from last thread I posted, as that has the most extensive research on the topic.

I recommend anyone reading this does not do this fix and reads the proposed links below to fully understand the problem and stop future compound issues

avatar image Greyeeh Daemonhahn · Nov 09, 2017 at 12:56 PM 0
Share

The thing is that my positions aren't desynced. The agent gets stuck on the edge of the navmesh and cannot reach the destination.

avatar image Daemonhahn Greyeeh · Nov 09, 2017 at 01:03 PM 0
Share

I know what the problem is, I have been actively researching and posting on the navmesh forums for the past 2 months every day with many other users, as youll see from those threads.

What I am saying is that what you think the problem is, it isn't. Yes its getting stuck on corners, but as you will see from those threads, that is a symptom of the actual navmesh positioning being incorrect, both within the navmesh and within sampleposition.

Warping the agent ensures the place it thinks it is at, is where it actually is at.

It is one line of code that you can add to ensure that the agents work correctly, so rather than ignoring it I would really read through what ive said or in a week or two there will be yet another navmesh forum post asking the same questions, with me answering yet again for the 4th week in a row with the actual fixes. If you actually read the posts I had given as well as gone through the navmesh forum, youll see that many others have thought it was something else, only to in the end find that warping is the only true way to get a 100% accurate reported position in the current version.

Anyway its your game, but I am saying add one line to everything you have and other compound issues wont rear their head later. You can ignore what I am saying constantly, but in the end adding in this line of code does not change ANYTHING other than fix potential issues, so it seems pointless to ignore it?

$$anonymous$$y 2 cents.

avatar image
1

Answer by Daemonhahn · Nov 09, 2017 at 11:34 AM

Currently the navmesh agents are bugged, please see these threads:

https://forum.unity.com/threads/agents-get-stuck-on-navmeshlink.503527/

https://forum.unity.com/threads/agent-getting-stuck-in-another-agent-when-walking-in-corners-what-is-the-right-solution.501824/

https://forum.unity.com/threads/strange-navmeshsurface-behavior.501453/

https://forum.unity.com/threads/failed-to-create-agent-because-it-is-not-close-enough-to-the-navmesh.500553/

https://forum.unity.com/threads/navmesh-link-does-not-connect-properly-in-runtime.473223/

Until they fix it, there are a ton of problems with it.

Comment
Add comment · Show 4 · 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 Greyeeh · Nov 09, 2017 at 11:52 AM 1
Share

Well, that is disappointing. Thank you for your answer.

avatar image Daemonhahn Greyeeh · Nov 09, 2017 at 12:05 PM 0
Share

No problem, please select my answer as the correct one if I have helped you! :)

avatar image Greyeeh Daemonhahn · Nov 09, 2017 at 12:07 PM 0
Share

I actually managed to solve it.

Show more comments

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

85 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

Related Questions

NavMeshAgent's desiredVelocity and .hasPath falsely returning 0 / false 2 Answers

NavMeshAgent dont find the right way on runtime build NavMesh ,Agents didn´t walk right on a runtime generated NavMesh 0 Answers

When I set Obstacle Avoidance Radius of NavMesh in Unity, the unit defuse to move?,I set Avoidance Radius in NavMesh Agent, and my unit refused to move? 0 Answers

Navmesh two agents going to the same possition 0 Answers

Can't get NavMeshAgent move. NavMeshAgent ignores destination property 7 Answers


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