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 /
avatar image
0
Question by nicoolsen10 · Jan 31, 2020 at 02:10 PM · 3dlistenemybuilding

Finding enemies from a list script

Hey I have a building that I want to find the nearest enemy via another scrip I have where I store the enemy's how do I make the building get the enemy list and then find the nearest enemy

i have defined the enemy list enemiesManager = FindObjectOfType();

this is my targetfinder where the enemies should be from the enemies list

   public void TargetFinder()
         {
             currentTargetDistance = Mathf.Infinity;
             foreach (var x in enemies)
             {
                 float distance = Vector3.Distance(transform.position, x.transform.position);
     
                 if (distance < currentTargetDistance)
                 {
                     myTarget = x;
                     displayText.text = "My target: " + x + "\n Distance to Target: " + distance; //skal fjernes nå det er helt klart
                     currentTargetDistance = distance;
                 }
             }
         }
 

this is the enemy list

  public class EnemiesManager : MonoBehaviour
     {
         public List<GameObject> _allEnemies = new List<GameObject>();
     
         public static EnemiesManager Current;
     
         private void Awake()
         {
     
             if (Current == null)
                 Current = this;
         }
     }
 
 




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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by austephner · Jan 31, 2020 at 04:00 PM

There's a bazillion different ways to do target finding, so you just have to settle on the one that makes the most sense for your project. One that I find easy for prototyping and spinning up new projects quickly is the singleton pattern - create a static reference out of EnemiesManager such that any TargetFinder can quickly and easily access the current list. You could even have a method inside of the manager class that takes a position and automatically finds the closest enemy to that position. Example:

 public GameObject GetEnemyClosestToPoint(Vector3 point) 
 {
     GameObject closestEnemy = null; 
     float closestEnemyDistance = 0.0f; 
     
     if (_allEnemies.Count > 0) 
     {
         // by default grab first enemy 
         closestEnemy = _allEnemies[0]; 
         closestEnemyDistance = Vector3.Distance(closestEnemy.transform.position, point);
     }
     
     foreach (var enemy in _allEnemies) 
     {
         if (enemy == closestEnemy) continue; 
         
         var distance = Vector3.Distance(enemy.transform.position, point); 
         
         if (distance < closestEnemyDistance) 
         {
             closestEnemy = enemy; 
             closestEnemyDistance = distance; 
         }
     }
     
     return closestEnemy;
 }
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 austephner · Jan 31, 2020 at 04:11 PM 0
Share

looks like you're already using the singleton pattern, my mistake. But you're on the right path in my opinion!

avatar image nicoolsen10 austephner · Jan 31, 2020 at 04:26 PM 0
Share

I found out how to do it but thanks for your help :)

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

159 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

Related Questions

A node in a childnode? 1 Answer

Mario first person game, help? 1 Answer

Canduct and movement of an enemy 0 Answers

Make an enemy follow along a wall 2 Answers

Problem with adding items to a list 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