Agent stops at every waypoint

So, i have a script where the player object follows a certain path, this is the script:

using System.Collections;
using UnityEngine;

public class PlayerMover : MonoBehaviour
{
    public float movingSpeed = 2.0f;
    public float minWaypointDistance = 0.1f;
    public Transform[] waypoints;

    private NavMeshAgent nav;
    private int curWaypoint = 0;
    private int maxWaypoint;

    private void Awake()
    {
        nav = GetComponent<NavMeshAgent>();
        maxWaypoint = waypoints.Length - 1;
        nav.speed = movingSpeed;
    }

    private void Update() //Every frame let it patrol
    {
        if (EnableDriving == true)
        {
            Patrolling();
        }
    }

    public void Patrolling()
    {
        //Temp positions
        Vector3 tempLocalPosition;
        Vector3 tempWaypointPosition;

        tempLocalPosition = transform.position;
        tempLocalPosition.y = -1.5f;

        tempWaypointPosition = waypoints[curWaypoint].position;
        tempWaypointPosition.y = -1.5f;

        if (Vector3.Distance(tempLocalPosition, tempWaypointPosition) <= minWaypointDistance)
        {
            if (curWaypoint == maxWaypoint) //When waypoint is max waypoint, put it on 0 again.
            {
                 EnableDriving = false;
            }
            else //If not last, let it move to next waypoint
            {
                curWaypoint++;
            }
        }
        nav.SetDestination(waypoints[curWaypoint].position);
    }
}

But, every time it reachs a different waypoint, he stops, turns to the other waypoint, and starts driving, how would you make it he either keeps driving or atleast doesnt completly stop?

Edit: Forgot to say, i tried auto breaking off, but then if he makes a to sharp turn, he keeps spinning around

I had a similar issue. My agents has a float called proximity, which could be any number. I then do a distance check between a navmeshagent and the next way point; if the distance is less than the proximity, change the navmeshagent’s destination.

Basically change to the next waypoint before the the agent reaches the waypoint

1 Like

Hey, Thanks for the answer! In the end i did a combination of stopping distance and the proximity it had to be on the waypoint, not the perfect solution but it does the job.