Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by BravenBitSoftware, Aug 13, 2017.

  1. BravenBitSoftware

    BravenBitSoftware

    Joined:
    Feb 26, 2014
    Posts:
    56
    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.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
    lordofduct likes this.
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    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
     
  4. BravenBitSoftware

    BravenBitSoftware

    Joined:
    Feb 26, 2014
    Posts:
    56
    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?
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  6. BravenBitSoftware

    BravenBitSoftware

    Joined:
    Feb 26, 2014
    Posts:
    56
    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.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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?".
     
    Kurt-Dekker likes this.
  8. BravenBitSoftware

    BravenBitSoftware

    Joined:
    Feb 26, 2014
    Posts:
    56
    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.
     
  9. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,385
    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.
     
    lordconstant likes this.
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This code will do the job for you. I've generalized it to include as many targets as you would like in the game.

    Code (CSharp):
    1. foreach (Enemy enemy in EnemiesICanSee){
    2.     Attack(enemy);
    3. }
    If you want to limit it to two targets you can do so this way.

    Code (CSharp):
    1.  
    2. int noOfAttacks = 2;
    3. foreach (Enemy enemy in EnemiesICanSee){
    4.     if (noOfAttacks <= 0) break;
    5.     Attack(enemy);
    6.     noOfAttacks--;
    7. }
     
    lordconstant likes this.
  11. malcontent93

    malcontent93

    Joined:
    Feb 4, 2021
    Posts:
    1
    I don't suppose you'd be willing to share some insight on how you're approaching having them prioritize targets?