Search Unity

Object movement jittery using navMeshAgent following moving player

Discussion in 'Navigation' started by f2pallglory, Mar 9, 2019.

  1. f2pallglory

    f2pallglory

    Joined:
    Jan 11, 2019
    Posts:
    25
    Hi guys ive posted this on scripting subforum but this subforum probably suit best, i got a problem when i use navmeshagent to my enemy to follow my player , i use 2 simple object player is sphere and enemy is cube.
    The problem is enemy movement when following my player when moving is so jittery i am confused how it became like this , has tried reducing stopping distance, disabling auto brake, and changed to fixedupdate but problem still occur , like this
    bandicam20190309120226982.gif


    i dont use rigidbody in the object , or is there a way we can move our object with rigidbody but still using our navmesh path? i saw someone said it could fix this problem , or is there something wrong with my code :)?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.AI;
    3.  
    4. public class EnemyMovement : MonoBehaviour {
    5.  
    6.     public NavMeshAgent ai;
    7.     public float distance;
    8.     public float timer;
    9.     public float wanderTimer;
    10.     public float stoptimer;
    11.     public float currTimer;
    12.     public float wanderRadius;
    13.     public bool isStop;
    14.     public int numPoint;
    15.     public Transform[] patrolPoint = new Transform[5];
    16.     GameObject player;
    17.     NavMeshSurface ns;
    18.  
    19.  
    20.     void Start () {
    21.  
    22.         player = GameObject.Find("Player");
    23.         isStop = false;
    24.         stoptimer = 3f;
    25.         wanderTimer = 8f;
    26.         numPoint = 0;
    27.    
    28.     }
    29.  
    30.     void Update () {
    31.  
    32.         if (isStop) {
    33.             stopAwhile();
    34.         }
    35.         else if (detectPlayer())
    36.         {
    37.             ai.SetDestination(player.transform.position);
    38.             ai.speed = 6;
    39.         }
    40.         else
    41.         {
    42.             if (!ai.pathPending && ai.remainingDistance <= 0f) {
    43.                 patrol();
    44.             }
    45.        
    46.         }
    47.     }
    48.  
    49.  
    50.     bool detectPlayer() {
    51.    
    52.         Collider[] det = Physics.OverlapSphere(transform.position, 15f);
    53.         for (int i = 0; i < det.Length; i++) {
    54.  
    55.             if (det[i].name == "Player") {
    56.                 return true;
    57.             }
    58.         }
    59.         return false;    
    60.    
    61.  
    62.     }
    63.  
    64.     void stopAwhile() {
    65.         currTimer += Time.deltaTime;
    66.  
    67.         if (currTimer >= stoptimer) {
    68.             isStop = false;
    69.  
    70.             currTimer = 0;
    71.         }
    72.  
    73.     }
    74.  
    75.     void patrol() {
    76.  
    77.         if (patrolPoint.Length == 0) {
    78.  
    79.             return;
    80.         }
    81.  
    82.         ai.SetDestination(patrolPoint[numPoint].position);
    83.         numPoint = (numPoint + 1) % patrolPoint.Length;
    84.  
    85.     }
    86.  
    87.  
    88.  
    89.     private void OnCollisionEnter(Collision collision)
    90.     {
    91.         if (collision.gameObject.name == "Player") {
    92.             isStop = true;
    93.             player.GetComponent<Player>().getDamage();
    94.        
    95.         }
    96.     }
    97. }
    98.  
    Is there any solution for this ?
    Thankyou so much
     
  2. htebbitt

    htebbitt

    Joined:
    Jan 5, 2019
    Posts:
    3
    I have a similar problem and haven't found any viable solutions yet. Were you able to eliminate the jitter?
     
  3. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    107
    Hi,

    Use this code for debug. And you will see, s it caused by re-pathing or Agent is getting weird.
    Code (CSharp):
    1.     var v2tmp =transform.position - Objective.transform.position;
    2.      if (v2tmp.sqrMagnitude > 0.5)
    3.      {
    4.         Agent.SetDestination(Objective.transform.position);
    5.         var size = Agent.path.GetCornersNonAlloc(subCorners);
    6.         DebugUtility.DrawEdges(subCorners, size, Color.green);
    7.     }
    8.     public static void DrawEdges(Vector3[] worldPoints, int size, Color color, float duration = 0.01f)
    9.     {
    10.         // Draw each segment except the last
    11.         for (int i = 0; i < size - 1; i++)
    12.         {
    13.             Vector3 nextPoint = worldPoints[i + 1];
    14.             Vector3 currentPoint = worldPoints[i];
    15.             Debug.DrawLine(currentPoint, nextPoint, color, duration);
    16.         }
    17.     }
    18.