How to detect the nearest enemy?

I want to detect the closest enemy in a scene where there is more than one enemy, and I aim to use line trace to ensure that there is no obstacle in between, and if there is an obstacle, it will target the 2nd closest enemy. I can’t get out of the problem right now.

You can do it using EQS.