Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question NavMeshAgent stopping well before first destination & won't proceed to next destination

Discussion in 'Editor & General Support' started by cmitchell1313, Oct 26, 2020.

  1. cmitchell1313

    cmitchell1313

    Joined:
    Oct 13, 2020
    Posts:
    6
    Hello lovely experts!

    I have created an array of destination points for my Agent to follow along a patrol path. These 5 points have been set with empty gameobjects, placed relatively close to the ground, and assigned in the inspector. My agent proceeds towards the direction of the first point correctly, but whereas the intended behavior is that he will come very close to it and then calculate / follow to the next point, the Agent instead stops a good distance before reaching the first point. I'd say it stops distance of 10 before point 1, and it is supposed to reach distance 0.5 from point 1 and then calculate point 2 and move there.

    I got this code exactly from the unity documentation but unfortunately since I am new I don't see where this problem is being caused. Your help is very much appreciated!

    public class Patrol : MonoBehaviour
    {
    public Transform[] points;
    private int destPoint = 0;
    private NavMeshAgent agent;

    [SerializeField]
    private Animator animator;


    void Start()
    {
    agent = GetComponent<NavMeshAgent>();
    agent.autoBraking = false;
    GotoNextPoint();
    }

    void GotoNextPoint()
    {
    if (points.Length == 0)
    return;

    agent.destination = points[destPoint].position;
    // move agent to currently selected destination

    destPoint = (destPoint + 1) % points.Length;
    // choose next point in array as destination, cycle to start if necessary
    }



    void Update()
    {
    float currentSpeed = agent.velocity.magnitude;
    animator.SetFloat("Speed", currentSpeed);
    // manages my walk animation


    if (!agent.pathPending && agent.remainingDistance < 0.5f)
    GotoNextPoint();
    // choose next destination as agent approaches current one
    }
    }
     
  2. cmitchell1313

    cmitchell1313

    Joined:
    Oct 13, 2020
    Posts:
    6
    Solved! Stopping distance in the interface was the issue