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 NerdRageStudios · Feb 02, 2015 at 10:37 AM · gameobjectfindgameobjectswithtaggameobject.findgameobject.tag

Need help finding the closet player to an enemy

Hi, my game can have upto 4 players in it, and I am trying to work out how to determine which player is closest to an enemy, but I cannot seem to make the following script work.

I was trying to search for all tagged players, store them and then iterate through them one by one.

It seems that you cannot pass multiple tags into FindGameObjectsWithTag and that is where my problem is as my players are tagged as the following:

Player1 Player2 Player3 Player4

Here is the script I am trying to use (i have hard coded "Player1" into the FindGameObjectsWithTag as I only have 1 player at the mo, so I can test enemy AI.

    // Find the closest enemy 
    void FindClosestEnemy() 
    { 
      // Find all game objects with tag Player
      GameObject[] gos;
      gos = GameObject.FindGameObjectsWithTag("Player1"); 
      
      var distance = Mathf.Infinity; 
      var position = transform.position; 
      
      // Iterate through them and find the closest one 
      foreach (GameObject go in gos) 
      { 
          var diff = (go.transform.position - position); 
          var curDistance = diff.sqrMagnitude; 
          if (curDistance < distance) 
          { 
              closest = go; 
              distance = curDistance; 
          } 
      }
 
      target = closest;
 
     }

Please can someone help?

Comment
Add comment · Show 2
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 CodeElemental · Feb 02, 2015 at 10:40 AM 0
Share

If there is no need for real-time update of the closest enemy (like only get the closest before making the attack or smth), you could try Physics.SphereCast with a specific layer mask of the enemies (asu$$anonymous$$g they have colliders attached).

avatar image NerdRageStudios · Feb 02, 2015 at 11:01 AM 0
Share

I see what you mean, but I need to do this realtime as it sets the AI target of the closest player as they move around...

2 Replies

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

Answer by fafase · Feb 02, 2015 at 11:08 AM

if all your enemy have a common script like Enemy.cs you can find all active instances in the scene:

 void FindClosestEnemy() 
 { 
   // Find all game objects with tag Player
   Enemy[] gos = FindObjectsOfType<Enemy>(); // Change here
   
   var distance = Mathf.Infinity; 
   var position = transform.position; 
   
   // Iterate through them and find the closest one 
   foreach (Enemy go in gos)           // Change here the first type in the foreach (or use var instead)
   { 
       var diff = (go.transform.position - position); 
       var curDistance = diff.sqrMagnitude; 
       if (curDistance < distance) 
       { 
           closest = go; 
           distance = curDistance; 
       } 
   }
 
   target = closest;
 
  }
Comment
Add comment · Show 7 · 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 NerdRageStudios · Feb 02, 2015 at 11:10 AM 0
Share

Thanks, but I am not looking to find all objects with a common tag. I am trying to find 4 objects, each with a seperate tag.

avatar image fafase · Feb 02, 2015 at 11:16 AM 0
Share

If you seek for help you need to read the answer. In the way I provide, there is no tag anymore, there is a type of script. If you have Georges (Guitar1), Paul (Bass), John (Guitar2) and Ringo (Drums), they have different names and different tags but they all four got a Beatle script on them:

 Beatle[] beatles = FindObjectsOfType<Beatle>();

beatles contains all four items.

avatar image NerdRageStudios · Feb 02, 2015 at 11:16 AM 0
Share

Ah, I see what you are trying to show, use find objects with Type and look for the player manager script (which is common).

I tried that, but I cannot create an array of GameObjects using that :(

avatar image NerdRageStudios · Feb 02, 2015 at 11:18 AM 0
Share

thanks for explaining, but that syntax doesnt work because you cannot create an array of script objects

avatar image fafase · Feb 02, 2015 at 11:22 AM 0
Share

Again, read the damn answer...or learn Unity (not being rude trust me). What you need is the Transform component since you are after the position, gameObject gets you there with

  gameObject.transform.position;

well you can do the same with any $$anonymous$$onoBehaviour component

 beatle.transform.position;

Did you notice line 10 of my answer?

Show more comments
avatar image
0

Answer by MurcsRoyce · Jul 16, 2018 at 08:05 PM

i don't use Var but i did find this document helpful. thanks

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

20 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

Related Questions

What's the most efficient way of finding child objects at runtime? 1 Answer

Is "GameObject.FindAllGameObjectsWithTag" related to the hierachy? 1 Answer

Find one inactive player (gameobject) 2 Answers

Toggling a game object between an active and inactive state 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 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