As a part of my game, when the enemy does not see the player, it navigates randomly on the Navmesh. This works fine until it reaches a point where it would have to make a 90 degree turn into a hallway or in/out of a room. I’ve captured what the issue looks like here (ignore the constant rotations of the enemy, it is set to constantly look at the player). The path for the agent is to move from that smaller room into that larger room to the right.
Now, the enemy will eventually be able to make it out of this, but this can take quite a bit of time. Time in which the player can navigate through the game’s goals without having to deal with the enemy. This bug can also be used to the player’s advantage for an easy escape from the enemy. Finally, this bug seems to occur at a more severe extent the faster the agent is set to move, and not at all if the agent is slow enough.
As for the variables that the agent had at the time of this, you can click here to see a capture of what those were set to.
Finally, here are my code snippets for this simple random navigation system. Of course, these snippets are in a script attached to the enemy.
nav.speed = GlobalVars.gameDifficulty * (waypointSpeedMultiplier - waypointSpeedMultiplierSubtraction);
nav.acceleration = (GlobalVars.gameDifficulty * 1.3f) * (waypointSpeedMultiplier - waypointSpeedMultiplierSubtraction);
if ((!nav.pathPending && !nav.hasPath) || nav.remainingDistance < 15)
{
nav.SetDestination(RandomNavmeshLocation(100f));
}
private Vector3 RandomNavmeshLocation(float radius)
{
Vector3 randomDirection = Random.insideUnitSphere * radius;
randomDirection += transform.position;
NavMeshHit hit;
Vector3 finalPosition = Vector3.zero;
if (NavMesh.SamplePosition(randomDirection, out hit, radius, 1))
{
float dist = Vector3.Distance(hit.position, this.transform.position);
if (dist < 15f)
{
return RandomNavmeshLocation(radius);
}
else finalPosition = hit.position;
}
return finalPosition;
}
This is the only bug I’ve had with this so far but it is quite annoying. Once this issue is fixed, the enemy navigation will be flawless. Any ideas will help greatly, thanks ahead of time.