- Home /
The question is answered, right answer was accepted
Waypoints and out of range problem.[Solved]
Ok I have this AI over here thats a bit retarded at the moment and this is caused by it not being able to loop its waypoints.
As in it gets to the end of the last way point fine but then throws up the out of range exception. I need a way for it to make the current way point the first in the array again when it gets to the end.
Can you help me with a solution, I have been trying everything and they not working here is the code that so far.
if(inWaypointPath == true && curWaypointDist < 2){
currentWaypoint = currentWaypoint+1;
}
// this is what is not working... This is just one way I tried but failed.
if(currentWaypoint > wayPoints.Count){
currentWaypoint = 0;
}
Thanks.
Have you tried if(currentWaypoint >= wayPoints.Count)
or if(currentWaypoint > wayPoints.Count -1)
or even .Count +1?
just to be on the safe side. How do you actually use "currentWaypoint"? Like this?: wayPoints[currentWaypoint].position
Answer by Chris_Dlala · Sep 26, 2014 at 10:57 PM
Hi, @betaFlux is correct if you are using the array as expected. You are checking if the incremented index is past the last index in the array and then wrapping it around to the first index (0). The problem is that this will not reset the index to 0 until it is greater than the count but wayPoints[wayPoints.Count]
will be out of bounds due to indexing from 0.
if(currentWaypoint >= wayPoints.Count)
{
currentWaypoint = 0;
}
I hope that helps =D
Alright thanks to both of you guys it worked.
Thank you very much.
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Follow Player Via Waypoints 1 Answer
Array out of range 1 Answer
Object Pool only activating one prefab 1 Answer
Disable All Transform In List 1 Answer