I have a loop that runs to get AI characters from one point to another by finding the nearest actor in a designated direction, and moving to it. Once it arrives it runs the loop again.
9/10 times the characters take the exact same route each time, but sometimes it will return the nearest actor as one that is obviously much further than several others.
Here’s how it works. The optional “stops” are generic actors with no mesh or collision. They are added to an array.
The character arrives at the first stop, and a function runs that finds the nearest actor from an array of all possible stops. It compares the distance of that stop and the characters current stop to the point it entered from at the start. If the distance of the “nearest actor” is less than its current stop, it removes it from the array and rechecks it for the next “nearest actor” and so on.
This is NOT a For Each loop, thus is not skipping over indexes after removing one. It just goes from the “false” branch and is routed back prior to the previous node.
It works fine. But I’ll repeat it several times to test it, and even with nothing changing each test; every so often the “nearest actor” is most definitely not the correct pick.
Video Example
Dropbox - Find New Cover Bug.mp4 - Simplify your life
(Look for new cover for squad)
(Found it - Now determine the positions for each unit within squad)
(Move squad to new position - Check for enemies within range once there)
(Check for Enemies - If none found tell the rest of the squad)
(Less than half of the squad is within range of enemy units? Find new cover)