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 Paricus · Dec 17, 2016 at 03:39 PM · arraylistdelegatecomparesort

Sorting a list by distance to an object?

Hi. I think similar questions have been answered already, but I have had a hard time understanding those answers.

Basically, I want to sort a list of transforms by distance to the player, so the closest transform is at the top and of the list and farthest is at the bottom. I don't use lists that often so I'm not too familiar with their sort functions or how they work. A sample script or explanation would be greatly appreciated.

Thanks.

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
4

Answer by Paricus · Dec 17, 2016 at 04:51 PM

This ended up working for me, never used linq or OrderBy before so I'm still not too sure why this works but it does. If someone has a better method or can explain this method, I'd be happy to hear it.

    using UnityEngine;
     using System.Collections;
     using System.Linq;
     
     public class SortDistance : MonoBehaviour {
     
         public GameObject[] points;
         public GameObject sphere;
     
         void Start()
         {
             points = points.OrderBy(point => Vector3.Distance(sphere.transform.position, point.transform.position)).ToArray();
     
             foreach (GameObject point in points)
                 Debug.Log(point.name);
         }
     }


 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class SortDistance : MonoBehaviour
 {
 
     public GameObject sphere;
     public List<Transform> spawnPoints;
 
     void Start()
     {
         Sort();
     }
 
     void Sort()
     {
         spawnPoints.Sort(delegate (Transform a, Transform b)
       {
           return Vector3.Distance(sphere.transform.position, a.transform.position).CompareTo(Vector3.Distance(sphere.transform.position, b.transform.position));
       });
 
         foreach (Transform point in spawnPoints)
             Debug.Log(point.name);
     }
 
 }


This method also seems to work. Taken from http://answers.unity3d.com/questions/246781/sort-transforms-by-distance-to-player.html If anyone has the same problem.

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

65 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

Related Questions

Show Top 10 Of 1 GameType 2 Answers

How to arrange list 3 Answers

Sorting Variables Help 1 Answer

Matching Index of two Arrays after one Array is sort 3 Answers

[C#] Sorting a List of Gameobjects Alphabetically 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