i’m getting an out of array with this i dont understand why
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Path : MonoBehaviour
{
[SerializeField] List<Transform> wayPoints;
[SerializeField] float moveSpeed = 2f;
int waypointIndex = 0;
Transform initialPosition;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (waypointIndex <= wayPoints.Count) // if waypointIndex is less or equal to the total of elements in the list wayPoints - 1
{
var targetPosition = wayPoints[waypointIndex].transform.position; // targetPosition is the list of type Transform containing the waypoints | it starts at index waypointIndex which is = 0
var movementThisFrame = moveSpeed * Time.deltaTime; // gives movement speed
transform.position = Vector3.MoveTowards(transform.position, targetPosition, movementThisFrame); // this object position will move towards the targetPosition with movementThisFrame speed.
if(waypointIndex <= wayPoints.Count) // if the position of this object is equal to the targetPosition
{
waypointIndex += 1; // increase index of waypointIndex by 1 moving towards the waypoint with the respective index.
}
}
}
}