title: Unity's NavMesh Agent glitches on navmesh edges
body: <p>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. </p>
  
  <p>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. </p>
  
  <p>I've tried removing the Rigidbody completely, changing obstacle avoidance settings, lowering the maximum slope but nothing worked so far.</p>
  
  <p>This is how it looks like:</p>
  
  <p><a href="https://www.youtube.com/watch?v=8K0LkVcYm2Y&amp;feature=youtu.be&amp;t=1m30s" rel="nofollow noreferrer">Video 1</a></p>
  
  <p><a href="https://www.youtube.com/watch?v=tBYM93rXpiA&amp;feature=youtu.be&amp;t=45s" rel="nofollow noreferrer">Video 2</a></p>
  
  <p>The code I use to get random position:</p>
  
  <p><a href="https://pastebin.com/cf19iiz1" rel="nofollow noreferrer">Pastebin</a></p>
  
  <pre class="lang-c# prettyprint-override"><code>        void ControlRandomWander()
          {
              float pointDist = Vector3.Distance(currentWanderPos, transform.position);
  
              if(pointDist &lt; 2f || currentWanderPos == Vector3.zero)
              {
                  wanderWaitTimer += Time.deltaTime * 15;
  
                  anims.LookAround(true);
  
                  if (wanderWaitTimer &gt;= 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 &lt; pth.corners.Length - 1; i++)
                          {
                              pathDist += Vector3.Distance(pth.corners[i], pth.corners[i + 1]);
                          }
  
                          Debug.Log(pathDist);
  
                          if (pathDist &lt;= 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;
          }
  </code></pre>
  
  <p>I would really appreciate any help sorting out this issue.</p>