Unnamed's work was great, really.
As far as I know there is no way to know which unit has been
locked as a target. It would be great if it was.
Since as now you should be able to find target direction
(plus or minus deflection applied) this is fairly easy,even if not always
fruitful.
Check along the firing direction with a serie of nearestObject
commands, remember, if you are targeting air unit above all,
to use absolute sea level height. Space these checks about
15 meters from each other (even 70-80 if you are targeting
air units): if the target it's hidden among high trees, though,
this space between those checks should be reduced at least
to 5-6 meters.
Wanna know more ? Message me.