Search Unity

Question Best way to give enemy/ally AI tracking? (2D Top down)

Discussion in 'Scripting' started by Inverzus, Jul 15, 2020.

  1. Inverzus

    Inverzus

    Joined:
    Jun 26, 2020
    Posts:
    12
    Hi, I've been trying to make a 2D top-down space dogfighting game and am currently in the process of making some basic AI for my allies and enemies. I want the AI to start tracking the nearest game object that belongs to the opposing faction when they enter a certain range around the AI. Since I want the battle to be on a large scale, with dozens of ships on the map, what would be the most efficient to accomplish this? Currently, I'm using tags to keep track of the factions
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Typically this is accomplished with a trigger collider on a child GameObject. You can check the tags of the things that enter/exit the trigger in OncollisionEnter/Exit. Look into layer based collisions too.
     
  3. Inverzus

    Inverzus

    Joined:
    Jun 26, 2020
    Posts:
    12
    So from what I understand, what you mean is to give the AI-controlled ship a Collider2D, probably a circle collider, that extends out in a set radius around the ship and set it as a trigger. Have the AI script check the tags of whatever that enters/triggers the circle collider to determine whether it should start tracking or not?

    Currently, I've put the factions on its own layer such that there is no friendly fire or collision, (enemy cannot interact with enemy etc.)
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Precisely.