How to make unit priority targeting system?

Hey, I’m new to unity and game programming and I’m currently planning a 2D game with similar game mechanics as with League of Legends. Would anyone know how to make the basic units have a targeting system for e.g. The basic unit, when encountering multiple enemies, would prioritize attacking the player, then other basics units, then least priority is to attack the enemy stronghold/nexus. If you could help me out with the code or give a link to a relevant tutorial website/video, it would mean a lot to me. :slight_smile:

Step 1 you have a enemy script that handles the enemy (tanks, light or whatever) and player
Step 2 you have a enemy manager that has a STATIC enemy list that stores current enemies in the world.
Step 3 you got the enemy that has a coroutine that runs every .25f seconds

private IEnumerator SearchForEnemies(){
     while(gameobject != null){
           yield return new waitForSeconds(.25f);
           ArrayList<enemy> priority = new ArrayList<enemy>(); //THIS IS NOT OPTIMIZED, YOU SHOULD CREATE THE ARRAY BEFORE AND THEN CLEAR IT HERE
           foreach (enemy in EnemyManager.enemy)
           //HERE YOU DO CALCULATIONS BASED ON YOUR ENEMY BEHAVIOUR
           //EXAMPLE:
               if (enemy.tipe == 0) //0 is the player, 1 light, 2 thank
               {
                      target = enemy
               }else{
                     if (enemy.tipe == 1) 
                          priority.Add(enemy);
                    else //IF ENEMY TYPE = 2 CAUSE WE ONLY HAVE 0-1-2
                         priority.Insert(0, enemy);
              }
              target = priority.Get(0);
     }

}

STEP 4: you do calculations based on the target (enemy) variable like a takedamage function and you call it if the target distance is less that a range amount

THERE COULD EASILY BE CODE ERRORS UP THERE CAUSE I DID NOT COMPILE THIS, I JUST WROTE IT

Note: I did this in a console application. The best way would be to use enums as priority types, and order the list based on the enum type and its position in the priority enum collection. For example, start by defining your priorities:

public enum Priorities
{
    Player,
    Enemy,
    Other
}

Where player is the main target, then enemies, then other.


Then, create an interface that your object classes can inherit, which define its type. For example:

// Base interface for all objects detected by tower
public interface ITarget
{
    public Priorities Priority { get; }
}

Your object classes should inherit from the interface, and have its priority set. For example:

public class Player : ITarget
{
    public Priorities Priority => Priorities.Player;
}

(If your class derives from monobehaviour, write it like this:)

public class Player : MonoBehaviour, ITarget
{
    public Priorities Priority => Priorities.Player;

    void Start()
    {
        //...
    }
}

Finally, create a list of ITargets and sort them by the enum value. E.g:

// List of targets the tower can see
var targets = new ITarget[]
{
    new Enemy(),
    new Villager(),
    new Enemy(),
    new Player()
};

// Orders the target array by priority -
// Players at the top, then enemies, then other
targets = targets.OrderBy(x => (int)x.Priority).ToArray();

// Print all targets (OUTPUTS: Player, Enemy, Enemy, Villager)
foreach (var target in targets)
{
    Console.WriteLine(target.Priority.ToString());
}

Full Console Application

class Program
{
    static void Main()
    {
        // List of targets the tower can see
        var targets = new ITarget[]
        {
            new Enemy(),
            new Villager(),
            new Enemy(),
            new Player()
        };

        // Orders the target array by priority -
        // Players at the top, then enemies, then other
        targets = targets.OrderBy(x => (int)x.Priority).ToArray();

        // Print all targets (OUTPUTS: Player, Enemy, Enemy, Villager)
        foreach (var target in targets)
        {
            Console.WriteLine(target.Priority.ToString());
        }
    }
}

public enum Priorities
{
    Player,
    Enemy,
    Other
}

// Base interface for all objects detected by tower
public interface ITarget
{
    public Priorities Priority { get; }
}

public class Player : ITarget
{
    public Priorities Priority => Priorities.Player;
}

public class Enemy : ITarget
{
    public Priorities Priority => Priorities.Enemy;
}

public class Villager : ITarget
{
    public Priorities Priority => Priorities.Other;
}

Hope that helps @Artimose