correct priorities for targeting

I wrote logic for a turret that searches for enemies in a radius, adds them to an array and selects one mob as a target and attacks. The problem is that I can’t understand how to correctly set up priorities so that the gun selects mobs that are going ahead and if they go beyond the attack range, it looks for a new target, but not randomly, but those that were following the target from behind. In fact, I need logic by which the gun will select a target that is closer to the finish line, but I can’t understand how to implement this. game tower defender



ScanEnemy