Find Nearest Actor Inconsistent Results

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)

You should show your blueprint.

Done. Added to the original post. I also added a video of it.

At 0:16, they start running towards the right cover, but they change the direction after half a second. It looks the code finds the correct cover, but then it runs again before the soldiers reach it.

Nice! There is a delay after the move node in the AI Controller. This caused it to occasionally run the range check after it had already been executed by other characters within the same squad.

I removed the delay and range check function upon move completion, and added it to an overlap event on the cover_BP

Thank you