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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by byviolet · May 18, 2015 at 10:34 AM · unity5

finding closest n objects

I have this code so far, which works fine:

     public GameObject FindClosestWaypoints(){
         nameOfWaypoint = MouseClick.clickedWaypoint;
         Debug.Log(nameOfWaypoint);
         positionOfGetWaypoint = GameObject.Find (nameOfWaypoint).GetComponent<Transform>();
 
         waypointLocations = GameObject.FindGameObjectsWithTag("Waypoint");
         float distance = Mathf.Infinity;
         foreach (GameObject x in waypointLocations){
             Vector3 diff = x.transform.position - positionOfGetWaypoint.transform.position;
             if(x.transform.position != positionOfGetWaypoint.transform.position)
             {
                 float curDistance = diff.sqrMagnitude;
                 if (curDistance < distance){
                     closestWaypoint = x;
                     distance = curDistance;
                     
                 }
             }
 
 
 
 
         }
         Debug.Log ("The closest waypoint was found and it is located at " + closestWaypoint.transform.position);
         return closestWaypoint;
 
 
     }

However, the problem with the code is that I only got it to return the closest waypoint gameobject. If I want it to return the closest 2/3/4/5 game objects, how would I do so?

Comment
Add comment · Show 6
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 $$anonymous$$ · May 18, 2015 at 10:45 AM 0
Share

Upon finding a close point you could add it to a list/array, then for each subsequent close point you could deter$$anonymous$$e if you are under your required point count, in which case add it to the list, or if you have enough points, compare the new points distance to all of the close points. if it is closer then you can replace the old point with the closer one. Repeat until you run out of points.

Hopefully that was some what clear to you...

avatar image $$anonymous$$ · May 18, 2015 at 10:48 AM 0
Share

You can also use a OverlapSphere (or similar) raycast in order to only get waypoint objects within your desired range. It might reduce the number of waypoints you have to sift through.

avatar image Cherno · May 18, 2015 at 10:55 AM 1
Share

^This would rquie the waypoint objects to have trigger colldiers, though, and the player object having a way to register those.

BTW, here is some sample code that sorts GameObject based of their distance.

 objectList.Sort(delegate(GameObject c1, GameObject c2){
                 return Vector3.Distance(this.transform.position, c1.transform.position).CompareTo
                     ((Vector3.Distance(this.transform.position, c2.transform.position)));   
             });
avatar image Owen-Reynolds · May 18, 2015 at 03:33 PM 1
Share

RougelikeShadow: that's a Selection Sort (that's the official name for a standard sorting method.)

The way to find "first N" is to sort the entire list, then take the first N of them. Obviously, you pre-compute and save distances, ins$$anonymous$$d of re-computing for each compare.

A Selection Sort naturally works by finding the closest, next closest ... so it's easy to stop it after you're gotten just the first N.

Cherno's solution is using a faster sort (N log(N).) But sorts the entire list.So might be faster or slower depending on sizes (first 5 out of 10, or out of 1000?) But, looks like it computes distance over and over for each compare, so won't run as fast as it could.

avatar image $$anonymous$$ · May 18, 2015 at 04:21 PM 0
Share

Thanks that's actually really useful to have an official name for that.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Cannot get Collider event of different object 1 Answer

Is it possible to update my unity 4.3 to 5.1? 1 Answer

Loading a ScriptableObject from an Asset at Runtime (after downloading + caching) 1 Answer

GetSpectrumData Obsolete? 1 Answer

Android Plugin with AppCompat Theme 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