I come up with this code and I try to get the waypoint + 1 when I reach the waypoint and it adds up till the last waypoint. I get Array is out of Index error. I guess I need and foreach loop of waypoints ?
Code:
public float speed;
public float turnSpeed;
public Transform[] waypoints;
private int currentWp = 0;
void FixedUpdate ()
{
if(waypoints.Length >= currentWp)
{
return;
}
player.position = Vector3.MoveTowards(player.position, waypoints[currentWp].transform.position, speed * Time.deltaTime);
var rotation = Quaternion.LookRotation(waypoints[currentWp].transform.position - player.position);
player.rotation = Quaternion.Slerp (player.rotation, rotation, turnSpeed * Time.deltaTime);
float distance = Vector3.Distance(player.position, waypoints[currentWp].position);
if(distance <= 10)
{
currentWp++;
}
}