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 OanhDv · Jul 16, 2017 at 09:43 AM · distanceminimum

distance shortest in the list

How to determine the shortest distance between the gameobjects in the list Thank you !

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

Answer by FlaSh-G · Jul 16, 2017 at 03:52 PM

You need to iterate over the list of GameObjects, then, for each GO, iterate over all GOs with a higher index and compare the distance. There sure is some fancy way to write this with Linq, but explicit loops will probably create a better understanding here.

 var shortestDistance = Mathf.infinity;
 GameObject go1;
 GameObject go2;
 for(var i = 0; i < myGameObjects.Length - 1; i++)
 {
   for(var k = i + 1; k < myGameObjects.Length; k++)
   {
     var distance = Vector3.Distance(myGameObjects[i].transform.position, myGameObjects[k].transform.position);
     if(distance < shortestDistance)
     {
       shortestDistance = distance;
       go1 = myGameObjects[i];
       go2 = myGameObjects[k];
     }
   }
 }
 
 Debug.Log(go1.name + " and " + go2.name + " are closest with " + shortestDistance + "m distance.");

The outer for loop iterates over all GameObjects except the last, and the inner loop then iterates over all GameObjects that come later in the list than the GO from the outer loop. This way, you don't compare a pair of GameObjects twice.

For every pair of GameObjects, we calculate the distance between them. If that distance is shorter than the previous shortest distance, we overwrite the shortest distance and remember the two GameObjects we are looking at.

Comment
Add comment · Show 4 · 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 OanhDv · Jul 18, 2017 at 02:27 AM 0
Share

how to select tag for this enemy ?

avatar image OanhDv · Jul 18, 2017 at 03:32 AM 0
Share

sr ,i want to compare the distance with player

avatar image FlaSh-G · Jul 18, 2017 at 10:29 AM 0
Share

So you want to compare only to one specific object? You can make it only one loop then.

 var shortestDistance = $$anonymous$$athf.infinity;
 GameObject closestGO;
 for(var i = 0; i < myGameObjects.Length; i++)
 {
   var distance = Vector3.Distance(transform.position, myGameObjects[i].transform.position);
   if(distance < shortestDistance)
   {
     shortestDistance = distance;
     closestGO = myGameObjects[i];
   }
 }

The list of GameObjects referenced by "myGameObjects" can be gathered any way you want. For example

 var myGameObjects = GameObject.FindGameObjectsWithTag("Enemy");

But you shouldn't call that function every frame in Update.

avatar image OanhDv · Jul 19, 2017 at 01:58 AM 0
Share

ok , thank you so much ! ^^

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

68 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

Related Questions

How to determine the minimum distance between objects? 1 Answer

FPS Controller Minimum Movement Increasing 1 Answer

Instantiate gameobject with minimum distance from another gameobject 1 Answer

calculating vertical distance 1 Answer

code for detecting if a 2d infinite runner player stopped moving because it hit a wall or something 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