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
1
Question by SrBilyon · Feb 11, 2011 at 08:25 AM · aienemydistancestatestargets

Suggestions for AI Selecting From Multiple Targets?

I am setting up AI state system for my game and I have all the normal functions that an action battle system would have: Pathfinding, Movement, and Attacking . This system of mind is working well (For a one player game).

However, I will be having the player be assisted by 3 other partner AI (using the same scripts). Now, the problem is, the enemy's are locked on to the player object only, and the partner AI are targeting only the one enemy entity selected in the inspector.

I was wondering on what approach I should go towards getting the enemy to select a target between the 3-4 players and vise-versa for the partner AI.

So far, I have code from my targeting system that allows the player to find all of the closest enemies and selects it as a target. I am not sure how to get the enemy to use this same function.

var closest : GameObject;

function FindClosestEnemy () : GameObject { // Find all game objects with tag Enemy enemyLocations = GameObject.FindGameObjectsWithTag("Enemy"); //var closest : GameObject; var distance = Mathf.Infinity; var position = transform.position; // Iterate through them and find the closest one for (var go : GameObject in enemyLocations) { var diff = (go.transform.position - position); var curDistance = diff.sqrMagnitude;

             if (curDistance < distance) 
             { 
                 closest = go; 
                 distance = curDistance; 
             } 
         } 
     return closest; 
 }

I would conclude that maybe the following would be appropriate to make an enemy/partner change there target?

-Closest Enemy -Health Checking -Stat Checking (IDK) -Death?

Would a timer suffice for changing targets?

I would also need the Partners to set the player as there target when I'm fleeing from enemies or when the enemies are all dead.

Comment
Add comment · Show 3
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 PrimeDerektive · Feb 11, 2011 at 02:47 PM 0
Share

Is it a multiplayer game, using the built-in networking?

avatar image SrBilyon · Feb 11, 2011 at 03:40 PM 0
Share

No, this is a one-four player game running one computer (for now)

avatar image bananaboy · Sep 12, 2011 at 11:13 PM 0
Share

may i know if where can i put on this script my attack codes?? please.

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by StephanK · Feb 11, 2011 at 08:43 AM

Basically you can use the same function for enemy AI. You will just have to modify the tag it's searching for. I would just change the signature of the function to FindClosestEnemy(string tag) and use the same function for players and enemies.

Comment
Add comment · Show 2 · 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 SrBilyon · Feb 11, 2011 at 09:47 AM 0
Share

Facepalm That takes care of the enemies finding the locations of the players. But now, I just need to figure out the rest of the logic.

avatar image StephanK · Feb 12, 2011 at 11:07 AM 0
Share

I'd probably implement this as some kind of filter, where each filter is basically a function that takes a list and returns a list of enemies after filtering out certain conditions. So you could do something like this: 1. find relevant enemies. 2. filter out enemies too far away. 3. remove enemies that are "too strong" 4. find the closest of the remaining enemies

avatar image
0

Answer by bananaboy · Sep 12, 2011 at 09:22 PM

Hello there, what about the attack script to be include to that script so that when it finds multiple targets it would immediately attack them by projectiles. Please help me with this. because i'm having trouble in using this script.

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
0

Answer by Ziron999 · Feb 01, 2015 at 02:06 PM

 void DistanceToTarget()
     {
         //Sort so nearest enemy is always Enemies[0]
         PlayerUnits.Sort(delegate( Transform t1, Transform t2){
             return Vector3.Distance(t1.position,transform.position).CompareTo(Vector3.Distance(t2.position,transform.position));
         });
     }
 
 void TargetedEnemy()
     {
         DistanceToTarget();
         //after sorted nearest enemy will be 0
         SelectedTarget = PlayerUnits[0];
     }

This can handle around 80 units for both players/enemies if done right with collisions. If someone knows of a better optimized way please let me know!

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

2 People are following this question.

avatar image avatar image

Related Questions

SphereCastAll sees for miles 2 Answers

Following the next item in an array 0 Answers

2D Random AI 1 Answer

free roaming enemy ai 1 Answer

Top Down Shooter Enemy AI using raycasting 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