- Home /
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:
The code I use to get random position:
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.
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;
}
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
The thing is that my positions aren't desynced. The agent gets stuck on the edge of the navmesh and cannot reach the destination.
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.
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/strange-navmeshsurface-behavior.501453/
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.
No problem, please select my answer as the correct one if I have helped you! :)