What Happens When Two Enemies Are Equally Close to a Single-Target Attack?
Asked by
VelvetOrbit42
My small team is planning to make beat 'em ups and fighting games, and I'm trying to understand how targeting behaves in an edge case. Imagine an action-combat game where an attack is programmed to affect only one enemy: specifically, the enemy closest to the attacker. If two identical enemies are perfectly overlapping and both are exactly the same distance away, what would happen when the attack is used? Would the game select one of them, affect both, produce an error, or behave unpredictably? I'm asking what the result would be if no special handling had been added for this situationโnot how to prevent the overlap.
Answers
If the code doesnโt define a tie-breaker, it may look random even though itโs following a hidden ordering. One enemy might consistently win because it was created first or appears first in the engineโs query results, while a different implementation might choose the other one. Depending on the API and assumptions in the code, an invalid result or error is also possible, but two overlapping targets donโt inherently cause the game to fail.
Reply to this answer
There isnโt one universal result because it depends entirely on how the targeting code is written. The engine usually wonโt automatically โfreak outโ; your code will follow whatever ordering and comparison rules you gave it. If the logic assumes there can only be one closest target, the result could be an exception or an arbitrary selection.
Reply to this answer
In a typical implementation, the game checks candidates in some order and keeps the best match found so far. Since both enemies have the same distance, the first one encountered may remain selected. The order could come from a list, entity registration order, collision-system results, or something similar. If the comparison uses a strict less-than check, the first candidate usually wins; using less-than-or-equal could cause the later candidate to replace it.
Reply to this answer
Add your answer
Answers are reviewed before publication.
VelvetOrbit42: That makes sense. Iโm not familiar with the implementation details yet, which is why I wanted to know what kinds of outcomes are possible.