Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by DimitriUK · Aug 22, 2017 at 05:14 PM · c#ailistenemyvector3.distance

Detecting Enemy Distance via List

I have been attempting to create a system that detects all enemies inside a sphere collider and calculates their distance. So far, I have this which grabs all the objects and puts them in to a list, this works good.

 private void OnTriggerEnter(Collider other)
     {
 
 
         if (HitBoxAI.isENEMY) {
             if (other.tag == "Friendly")
             {
                 if (!targets.Contains(other.gameObject))
                 {
                     targets.Add(other.gameObject);
                 }
             }
             if (other.tag == "Player")
             {
                 if (!targets.Contains(other.gameObject))
                 {
                     targets.Add(other.gameObject);
                 }
             }
         }
         if (HitBoxAI.isALLY)
         {
             if (other.tag == "Enemy")
             {
                 if (!targets.Contains(other.gameObject))
                 {
                     targets.Add(other.gameObject);
                 }
             }
         }


Now, that all works and successfully adds the gameobjects into the list.

What do I want?

I want to make it so that I can run a function that will look through the list and determine the closest game object in the list to the current transform.gameObject and then to assign the AI's target to that.

So, as for pseudocode, I am trying to achieve something like;

 ai.target = targets.closestInList


I can imagine another way would be using the List.Sort method and then just making the target the first in the index such as [0]. My experience with lists unfortunatetly is not very good, I hope someone can give a helping hand.

Many thanks.

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
Best Answer

Answer by SilverSho0t · Aug 22, 2017 at 05:35 PM

I do this a lot for my game, i'm happy to share it to you if it can help you ;) It's pretty simple.

float distance = 0;
GameObject closestTarget;
for (int i = 0; i < targets.Length; i++)
{
    float targetDistance = Vector3.Distance (transform.position, targets [i].transform.position);
    if (targetDistance < distance || i == 0)
    {
        closestTarget = targets [i];
        distance = targetDistance;
    }
}
ai.target = closestTarget;
I hope it help, Sean.

Comment
Add comment · Show 1 · 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 DimitriUK · Aug 22, 2017 at 05:47 PM 1
Share

Hey Sean!

Thanks for sharing that, I finally found a solution, however, yours is also correct so I'll mark you as correct! :)

For any others who come across this thread;

  1. Create a Script called GameObjectUtils and insert this code inside;

    using UnityEngine; using System.Linq; using System.Collections; using System.Collections.Generic;

          public static class GameObjectUtils
             {
                 public static List<GameObject> SortByDistance(this List<GameObject> objects, Vector3 mesureFrom)
                 {
                     return objects.OrderBy(x => Vector3.Distance(x.transform.position, mesureFrom)).ToList();
                 }
             }
    
     
    
    
  2. Create a function for your script with the list and run the function whenever you want it to sort them in to distance and then you could simply set your target to targets[0].gameObject.

    public void ByDistance() { targets = targets.SortByDistance(transform.position); }

avatar image
1

Answer by Malace · Aug 22, 2017 at 06:12 PM

You will have to loop through your list. Keeping track of which object has the lowest distance from the target object.

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class Example : MonoBehaviour {
     
         public List<GameObject> targets;//This is your list fill it with the objects you want to test.
         public GameObject NearestEnemyObject;// This will hold our result of our closest target at the end
         public GameObject ObjectToCompareTo;// This you will need to have declared as your object you want to be your center point.
         public float closestTarget;// used to detect the first run of our foreach loop below.
     
         // Use this for initialization
         void Start () {
     
            closestTarget = 0.0f;//Set to 0 so we can force the first run of the loop to catch our distance values.
     
             ///Assuming your list is already populated in this script before this point....
             foreach (GameObject TargetObjectToTestDistance in targets)
             {
                 if (closestTarget == 0.0f)//Only happens on first run as we are about to set below. We need to have at least one distance calculation before we can compare that vs the other objects in list.
                 {
                     closestTarget = Vector3.Distance(TargetObjectToTestDistance.transform.position, ObjectToCompareTo.transform.position);//Since we are in our fist run store the distance of the first object of our list
                     NearestEnemyObject = TargetObjectToTestDistance;//Go ahead and set our NearestEnemy variable incase the first object is the closes out of all.
                 }
                 else//this will trigger every run after the first since distance should never return 0.0f unless you test 2 objects in the same x,y,z location.
                 {
                     if (Vector3.Distance(TargetObjectToTestDistance.transform.position, ObjectToCompareTo.transform.position) < closestTarget)//test to see if each following object's distance is lower than our current lowest distance result
                     {
                         closestTarget = Vector3.Distance(TargetObjectToTestDistance.transform.position, ObjectToCompareTo.transform.position);//if the object was closer update our closest distance float so our next loop run will test vs this.
                         NearestEnemyObject = TargetObjectToTestDistance;//Since we are closer than any object before go ahead and store the gameobject for later use.
     
                     }
                 }
     
             }
             //Now that our loop has finished iterating through all our list objects we should have overwritten NearestEnemyObject with the closet target enemy of all from our list
 Debug.Log("Closest object is. " + NearestEnemyObject.name.toString());
         }
     
     }


This is just a rough example. But if you populate the list with some objects. Then set your object to test against through inspector you should end up with the name of the closest object in debug.

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

393 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 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 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 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 to search a LIST(not an array) for game objects with a specific tag? 1 Answer

Animations not playing correctly for FPS enemy AI 0 Answers

Animations not playing correctly for FPS enemy AI 0 Answers

Most efficient way to store information in an inspector dropdown menu? 0 Answers

Why does my enemy teleport to my player? 2 Answers


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