Search Unity

Bug NavMeshAgent teleports back when traversing NavMeshLink

Discussion in 'Navigation' started by Razputin, Mar 9, 2022.

  1. Razputin

    Razputin

    Joined:
    Mar 31, 2013
    Posts:
    356
    Sometimes when traversing a NavMeshLink my agents will snap back to one side or the other instantly. I'm not sure why this is happening. I made a basic movement script and video to demonstrate.

    Video Demonstration :


    How the NavMeshLink is setup:
    bbbbb.png

    Code to move capsule :
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.AI;
    6.  
    7. public class test : MonoBehaviour
    8. {
    9.  
    10.     public int dist = 4;
    11.     public NavMeshAgent agent;
    12.     public GameObject player;
    13.  
    14.     public void Update()
    15.     {
    16.         if (Vector3.Distance(transform.position, player.transform.position) > dist)
    17.         {
    18.             agent.SetDestination(player.transform.position);
    19.         }
    20.         else
    21.         {
    22.             agent.ResetPath();
    23.         }
    24.     }
    25.  
    26. }
     
  2. JavaBob

    JavaBob

    Joined:
    Apr 24, 2018
    Posts:
    20
    Because you called the ResetPath() method. This is what the official documentation says:
    upload_2022-3-15_21-37-19.png
    I think this applies to NavMeshLink too.
     
  3. Razputin

    Razputin

    Joined:
    Mar 31, 2013
    Posts:
    356
    This seems to have been the problem. Thank you!