- Home /
How do I determine the previous point that my enemy was at?
In a patrolling system I have what the current patrol point is, but I'd like to know what the previous patrol point is as well to make this work:
if (currentPatrolPoint.position.y < previousPatrolPoint.position.y) { anim.SetBool("walkDown", true); downSearch.SetActive(true);
This is what I have so far:
if (currentPatrolIndex == 0)
{
previousPatrolIndex = patrolPoints.Length;
}
else
{
previousPatrolIndex = (currentPatrolIndex - 1);
}
currentPatrolPoint = patrolPoints[currentPatrolIndex];
previousPatrolPoint = patrolPoints[previousPatrolIndex];
But the last line is pulling up an error that says, "Array index is out of range." Help please? Thanks!
Answer by Davirtuoso · May 12, 2017 at 12:37 PM
If the currentPatrolIndex was 0, this would set previousPatrolIndex to the length of your array. The problem is that array lengths start at 1 and array indexes start at zero, so where an array that has 5 indexes has a length of 5, the indexes range from 0-4.
To get around it, make your first if statement do this instead:
previousPatrolIndex = (patrolPoints.Length - 1);
This will then make sure it goes to the last entry in the array and not the one after (which doesn't exist). I hope that helps :)
Thank you! That fixed the error and the explanation was appreciated :)
Not a problem - I'm happy to be of assistance :)