Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Agent stops before reaching first destination in series, can't proceed to next stop

Discussion in 'Navigation' started by cmitchell1313, Oct 26, 2020.

  1. cmitchell1313

    cmitchell1313

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

    I have created an array of 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. Let's say it stops distance of 10 before point 1, and it is supposed to reach distance 0.5 and then calculate the next point.

    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! good old stopping distance was the culprit...
     
  3. bdriscoll3

    bdriscoll3

    Joined:
    Nov 22, 2022
    Posts:
    1
    And this solved my problem, LOL. Thanks