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