Search Unity

Navigation mesh agent works for 10 seconds, then starts moving sporadically

Discussion in 'Navigation' started by mdmiller002, May 16, 2017.

  1. mdmiller002

    mdmiller002

    Joined:
    Jul 27, 2016
    Posts:
    1
    I have a NavMeshAgent guiding one of the enemies in my game, getting it to wander around, and my code works for like 10 seconds, the enemy moves around fine, however after that the enemy starts jumping around the nav mesh, sporadically, with no fluidity or anything. This is the code that is called in my update method to keep the enemy moving

    Code (CSharp):
    1. private void enemyRoam()
    2. {
    3.     Vector3 randomDirection = Random.insideUnitSphere * wanderRadius;
    4.     randomDirection += transform.position;
    5.     NavMeshHit hit;
    6.     NavMesh.SamplePosition(randomDirection, out hit, wanderRadius, -1);
    7.     Vector3 finalPosition = hit.position;
    8.     agent.destination = finalPosition;
    9. }
    Any ideas on why this would work for a few seconds and then start moving the enemy sporadically?

    Thanks!