Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Artimose · Jun 10, 2021 at 10:23 AM · unity 2denemy aitargeting

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. :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Patrickmol · Jun 10, 2021 at 10:50 AM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Llama_w_2Ls · Jun 10, 2021 at 12:17 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

127 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I flip an enemy that's automatically walking and targeting the player? 1 Answer

My enemy gameobjects are merging into one when I want them to follow me in 2D, need some help. 1 Answer

enemy shakes when it gets to a certin position 0 Answers

Astar Pathfinding with unity 2d 1 Answer

"the object of type transform has been destroyed but you are still trying to access it" problem, 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges