How to make AI fight with more than one hostile at once?

Title. I know it must be difficult to explain, but i just wanted to know: How could i make an AI that fights with more than one hostile at once? Example: Let’s say i have a knight, and i want it to fight the player and it’s companion AI. How would i do it? Any ideas?

I am thinking about making some kind of “switching” between targets, but i don’t know, maybe you guys have a better way of doing it.

This is far too abstract of a question to provide an actual answer. First solve the game design section, what does fighting two enemies at once actually mean? Then we can look at code.

1 Like

Kiwasi is right, you are far too general,
i’m dealing with this problem at the moment, currently on medieval melee
what i basically do is have my warriors have a selection of what’s the priority(closest, strongest, weakest, etc.)
and make the unit recalculate the targets priorities every now and again(about 3-4 times a second right now, and that’s plenty)
also, if a unit get damaged the attacking unit will be targeted

Well… That’s… strange. I just can’t see how this would be vague, i am just asking how would you do it. The AI can be ranged, melee, it can be a wizzard, a necromancer, a hacker, it can fly, and none of these aspects would ever change my argument. What can i say to help?

I don’t even know what type of game you are making. An RTS would have a very different approach to a FPS game. A turn based tactical game would be different from a survival game.

Then you need to define the AIs behavior. When faced with two enemies, what should it do. Target the closest? Target the weakest? Target both at once? Find a fortified position? Run away? This is game design.

Good code flows from a good plain English description of the problem. Without that I can’t help you.

Well, i still don’t understand how would any of this info ever change anything about my quesion, but ok… Here you go:

I am working on a top-down view RPG. I am working on a melee AI right know to use with monsters, knights, etc. I am developing the game for mobile, and i am planning to make one hell of a big world and a lot of quests and all, so i don’t have much performance to spare a.k.a can’t make a complex AI.

“what should it do. Target the closest? Target the weakest? Target both at once? Find a fortified position? Run away?” That’s exactly what i meant when i asked it. How would i do it? I want ideas. Make an AI target someone is easy, the problem is what COULD i do to make it switch between targets. What i am using right now to make the AI identify a target is to detect if the player is on it’s “line of sight”, and then make it attack him. I can easily code the companion AI to attack multiple targets by simply making them attack a single target at once. When they are done, they automatically go to the next target. This same tactic is used on most games these days.

The thing is, how would i make an ENEMY AI fight the player and his companion AIs? I can’t just make them blindly attack a single companion or the player because they will obviously get killed in a blink so it would be better for the AI to attack multiple targets.

Got ya. In this case you are asking a game design question. Probably better to ask in that forum.

Game design is for “What should my game be?”.

Scripting is for “This is my game, how do I do it in Unity?”.

1 Like

Huh, if you can’t help me, just say it man, but don’t say such stupid things. Saying that my question would be better in the Game Design section is the same as if i asked about sound effects and someone pointed me to 3D Modelling.

It WOULD be in Game Design if i asked “Do i make my AI attack after X seconds, or Y seconds? Do i make it attack the Y target before the Z target, or the Z target before the Y target?” Which is NOT the case. I am asking HOW would i make an AI attack two targets at once a.k.a same match, which is literally and purely about Programming. You know what? Forget it. You just aren’t capable of helping me, i am wasting my time here.

You posted a very ‘design-y’ question with no code in the scripting forum, which basically revolves around people posting actual code. Questions and their Answers are generally pretty specific here.

To answer you with equivalent effort as you asked it: You must code the AI to analyze enemies it is aware of, then choose the highest priority action based on some process which you code to help it weight what is more important to do at specific times and under specific conditions. Often with Behavior Trees or FSM’s.

1 Like

This code will do the job for you. I’ve generalized it to include as many targets as you would like in the game.

foreach (Enemy enemy in EnemiesICanSee){
    Attack(enemy);
}

If you want to limit it to two targets you can do so this way.

int noOfAttacks = 2;
foreach (Enemy enemy in EnemiesICanSee){
    if (noOfAttacks <= 0) break;
    Attack(enemy);
    noOfAttacks--;
}
1 Like

I don’t suppose you’d be willing to share some insight on how you’re approaching having them prioritize targets?