- Home /
Enemy AI Patrol Points Array Index Out of Range?
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 );
//Should be less than equal ( <= ) not ( == )
if(transform.position <= patrolPoint[currentPoint].position)
{
currentPoint++;
}
That doesn't help, it gives me Assets/Scripts/Enemy/EnemyAttack.cs(27,30): error CS0019: Operator <=' cannot be applied to operands of type
UnityEngine.Vector3' and `UnityEngine.Vector3'
I thought you are checking distance. Sorry. What is happening here is that even though you have specified 3 positions. When you reach the third position the condition is true and increments the currentPoint to 4. Somewhere in this small time frame you are looking for patrolPoint[4] which does not exist. When the next frame starts currentPoint is reset to 0 and continues. Put a check condition and if the currentPoint is in limits then increment it.
An array index out of range error is when you are trying to access an array position that does not exist. You have three positions but when you reach the 3rd position you are accessing the 4th position in the array which does not exist. So put a condition before incrementing the variable.
Answer by Mmmpies · Jan 15, 2015 at 07:53 PM
You've just got the logic a bit wrong I think, try this:
if(transform.position == patrolPoints[currentPoint].position)
{
currentPoint++;
if(currentPoint >= patrolPoints.Length)
{
currentPoint = 0;
}
}
Answer by haim96 · Jan 15, 2015 at 07:32 PM
this is the problem:
if(currentPoint >= patrolPoints.Length)
say you have 4 points, "currentpoint" can be 0 to 3. but patrolPoints.Length= 4 because you have 4 element in your array.
you need to change it to:
if(currentPoint >= patrolPoints.Length-1)
I have three points. and if I use patrolPoints.Length-1 it removes the index array out of range error but then he just moves back and forth between the first two points and wont move on to the third.
if(currentPoint == patrolPoints.Length-1)
you right... this should do the trick...
if(currentPoint == patrolPoints.Length-1) still bounces him between the first two points...
@$$anonymous$$mmpies should work better... check his solution.
Your answer
Follow this Question
Related Questions
free roaming enemy ai 1 Answer
Different enemy types script design 1 Answer
Enemy keeps moving in one direction. Help pls! 2 Answers
How to I make FindGameObjectWithTag() not just find itself? 1 Answer
Enemy AI problems 2 Answers