So I have an AI service that finds all enemies within a maximum distance from the AI. It makes sure each possible target has more than 0 health and that it can be seen. I have three arrays, PossibleTargets(character array), TargetDistances (float array), and TargetHealth (float array). Which I have populated with each target, their distance to the AI, and their current health.
The question is: How do I find a target with the best of both worlds; the enemy that is the closest to the controlled AI and has the least health? (while still prioritizing the target that is closest)
It’s hard to say without knowing how your game works, but I think your implementation should depend on a few more factors than just distance and health. Anyway, I messed around with some values in a spreadsheet and I think I found a formula that works pretty well. The formula I used was distance * (health + a)[SUP]b[/SUP], and the values the seemed to give the best results were as follows:
a = 3 (lowering this value causes the health to be favored)
b = 2 (raising this value causes the health to be favored)
After this calculate is done, you would iterate through each enemy and pick the lowest value. Note that the health has to be normalized to a 0-1 range. However, if all of your enemies have the same max health, then you would simply find this value with health / max health.