My small team is planning to make beat 'em ups and fighting games, and I've been wondering how target selection works in an edge case. Suppose an attack is programmed to affect only one enemy—the closest enemy to the player—and two identical enemies are positioned perfectly on top of each other at exactly the same distance. If there's no special logic to prevent this situation, what would the game actually do? Would it select one of them, affect both, produce inconsistent results, or cause an error? I'm asking about the likely behavior of the unhandled tie itself, not how to prevent overlapping enemies.
4 Answers
It would only affect both enemies if the attack logic collects every target at the minimum distance or applies damage separately after finding the closest distance. If the code stores a single target and stops after selecting one, only one overlapping enemy gets hit. An error would require some separate bug, such as assuming the target list can never contain duplicates.
There isn’t one universal result; it depends entirely on how the target search is implemented. Most likely, the code will inspect candidates in some order and keep whichever one it encounters first. The game usually won’t crash just because two distances are equal—one target will simply win the tie according to the code’s existing ordering.
The result could appear random if the order of the enemies isn’t stable. For example, the winner might depend on registration order, entity IDs, physics-query ordering, or which object happens to be returned first. It’s not necessarily true randomness; it’s just an unspecified tie-breaker that can change between runs or systems.
A common implementation loops through the hitboxes, compares each distance with the current best distance, and replaces the target when the new distance is smaller. In that case, the first enemy checked remains selected when the distances are exactly equal. If the comparison uses less-than-or-equal instead, the later enemy will replace it, so the second one may be selected instead.

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically