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 IAmJeff · Feb 06, 2015 at 08:52 PM · gameobjectarraydistance

finding the closest player

I have an enemy and I want it to attack the closest player (with the tag "Player") within it's sight distance. I made it functional with without an array. But because the enemy will have multiple players. I want it to attack the closest player, if multiple players are within it's sight distance. So I've looked up FindGameObjectsWithTag and tried to combine both elements from the API and my own. But it's not functional as I want, so here's what I have:

 GameObject[] players;
 players = GameObject.FindGameObjectsWithTag("Player");
 
     void FindClosestPlayer()
         {
             if(IsAlive())
             {
                 GameObject closestPlayer;
                 float distance = Mathf.Infinity;
         
                 foreach(GameObject player in players)
                 {
                     //float distanceBetween = Vector3.Distance(transform.position, player.transform.position);
                     Vector3 difference = transform.position - player.transform.position;
                     float distanceBetween = difference.sqrMagnitude;
     
                     if(distanceBetween < distance)
                     {
                         closestPlayer = player;
                         distance = distanceBetween;
                         Debug.Log(distance);
     
                         //chase player if within range
                         if(distance < sightDistance && distance > attackRange)
                         {
                             transform.LookAt(closestPlayer.transform);
                             controller.SimpleMove(transform.forward * movementSpeed);
                             animation.CrossFade("Run");
                         }
                         
                         else
                         {
                             animation.CrossFade("Idle");
                         }
                         
                         Attack();
                     }
                 }
             }
         }
 

And in my debug statement, the distance is randomly generating numbers between the enemy and the player even though none of the two gameobjects are moving. The ranges I'm getting are anywhere from 6000+, 150+, 700+. Prior to doing this, my distance is calculating to be 15.

Comment
Add comment · Show 9
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 Glurth · Feb 06, 2015 at 09:00 PM 0
Share

I suspect you need to run though all the players FIRST, to find the closest. THEN attack whichever is closest.
The way you have it, if the player is closer than the previously checked player, it will attack that one (or go IDLE!). Constantly changing it's target, until it gets through the list.

using this difference.sqr$$anonymous$$agnitude; is fine (and fast) for comparing ditaances, but your debug statemtn is NOT showing the distance between the two objects, it's showing the Square of the distance, thats why they are so large.(eg, rather than 15, you see 15*15=225)

Not sure what you mean by this though: "But it's not functional as I want"

avatar image IAmJeff · Feb 06, 2015 at 09:24 PM 0
Share

Ah ok I was wondering why the values were so large. So I've changed my debug to:

 Debug.Log(Vector3.Distance(transform.position, closestPlayer.transform.position));

The issue I'm seeing in the debug is that despite the two gameobjects not moving the values that are being outputted are different each frame. And for now, I only have one object tagged "player". I know I need to added in a boolean so it only checks once.

avatar image Glurth · Feb 06, 2015 at 09:30 PM 0
Share

This is what I meant in my first paragraph-(in psudo code)- note: attack is after the loopend.

 closest=null
 loop()
  if(this one is closer than closest)
    closest=this one
 endloop
 attack(closest)


regarding those position values always changing- if they are parented to another object that DOES move, that could explain this. Debug.Log($$anonymous$$athf.Sqrt(distancesqrd).ToString()) - should give you the actual distance.

avatar image NickWu · Feb 06, 2015 at 09:40 PM 0
Share

Did you try to print out the position of corresponding gameObjects and see if you are getting the correct data?

avatar image getyour411 · Feb 07, 2015 at 01:16 AM 0
Share

It's not clear how often this is called, but if it's frequent you should create a cache $$anonymous$$yTransform variable (or whatever) so that, behind the scenes, it doesn't have to do resolve 'transform' each pass (see many posts on cache transform for more info).

Another alternative might be SphereCast or OverlapSphere executed in a coroutine so that it's not doing all this every pass

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

Find gameobject furthest from player along z axis 0 Answers

Calculate Distance Between two Game objects 1 Answer

HELP Find gameObject With tag in another array 1 Answer

Creating an array of prefabs? 4 Answers

Enabled all gameobjects in array 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