- Home /
An Array Question about removing
Hello everybody. I have an array question. I'm coding a waypoint script. I have waypoints in array, and everytime ai reaches a waypoint, I want it to random choose the next waypoint. But it can also choose the waypoint that ai is on already, so it goes crazy and starts rotating around waypoint object which is not cool you know.
I want to remove the current array and random around the rest, how can I do that ?
devriyeNoktasi = waypoints[arrayIndex]; // arrayIndex is an int, devriyeNoktasi is the position the ai will go, it chooses the arrayIndex numbered waypoint.
when a player reaches a waypoint, we have this code ;
arrayIndex = Random.Range(0,waypoint.length);
here is the thing, for example we have 4 objects, 0,1,2,3 . AI is on the element 3. The code above randoms from 0 to 4, which can include 3 too. So if it randoms 3 while ai is on 3, it rotates around itself which is not good. How can I remove the current array ? Thanks :)
Answer by Jamora · Jul 27, 2013 at 07:35 PM
I don't see a reason why you should delete you array. What you should be doing is remember the current location of the AI in you favorite location in code, then if the new location is the same as the old one, get a new random number. That way, you will never get the same waypoint. Pseudocode:
new AI waypoint = new Random number
while(AI current waypoint == new AI waypoint)
new AI waypoint = new Random number