I made a script to get my enemy to patrol between three different spawn points. It works, but when he reaches the third spawn point, even though he keeps going it gives me an Array Index out of Range error on line 31, the rotation to face the next patrol point?? Where did I screw up?
public class EnemyAttack : MonoBehaviour
{
public Transform[] patrolPoints;
public Transform target;
public int movementSpeed;
public int rotatationSpeed;
public float moveSpeed;
private int currentPoint;
void Start ()
{
transform.position = patrolPoints[0].position;
currentPoint = 0;
target = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update ()
{
if(currentPoint >= patrolPoints.Length)
{
currentPoint = 0;
}
if(transform.position == patrolPoints[currentPoint].position)
{
currentPoint++;
}
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(patrolPoints[currentPoint].transform.position - transform.position), rotatationSpeed * Time.deltaTime);
transform.position = Vector3.MoveTowards(transform.position, patrolPoints[currentPoint].transform.position, movementSpeed * Time.deltaTime );