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 D5R · May 19, 2019 at 07:51 AM · 3dpositioningsorting

How would you sort gameobjects by their position in a 3D grid?

Say I have a bunch of block gameobjects in a 3d grid space like in the picture below,

alt text

and they are stored as an array of gameobjects. My goal is to figure out a (hopefully simple) way of sorting them by their position in 3d space within the array such that the right-upper-top most is the first on the list and the left-lower-bottom most is the last in the array. It's kinda hard to explain but hopefully the image below shows the idea of how the blocks would be ordered in the array.

alt text

I can't just manually assign them the order in the beginning since the blocks can change their positions at runtime. Any ideas on how I would sort them in such an order would be highly appreciated.

captured.png (40.8 kB)
capturedf.png (49.0 kB)
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 Hellium · May 19, 2019 at 08:27 AM

The description of the "sort algorithm" you gave is different from the order you showed in the image (supposing on the image that the +Z axis is pointing up, and the +X axis is pointing right)


The following functions are meant to be used in the Array.Sort or List.Sort methods:


 System.Array.Sort( blocks, CompareBlocks ); // if blocks in a Vector3[]

 // OR

 blocks.Sort( CompareBlocks ); // if blocks in a List<Vector3>


Comparison function according to the image

 public static int CompareBlocks( Vector3 v1, Vector3 v2 )
 {
     // Comparing two vectors this way is fine
     // Unity has overloaded the == operator
     // So as to avoid floating point imprecision
     if ( v1 == v2 ) return 0;
 
     if ( Mathf.Approximately( v1.x, v2.x ) )
     {
         if ( Mathf.Approximately( v1.z, v2.z ) )
             return v1.y > v2.y ? -1 : 1;
         else
             return v1.z > v2.z ? -1 : 1;
     }
     return v1.x > v2.x ? -1 : 1;
 }


Comparison function according to the description

 public static int CompareBlocks( Vector3 v1, Vector3 v2 )
 {
     // Comparing two vectors this way is fine
     // Unity has overloaded the == operator
     // So as to avoid floating point imprecision
     if ( v1 == v2 ) return 0;
 
     if ( Mathf.Approximately( v1.z, v2.z ) )
     {
         if ( Mathf.Approximately( v1.x, v2.x ) )
             return v1.y > v2.y ? -1 : 1;
         else
             return v1.x > v2.x ? -1 : 1;
     }
     return v1.z > v2.z ? -1 : 1;
 }

Comment
Add comment · Show 1 · 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 D5R · May 19, 2019 at 08:56 AM 0
Share

Yeah I probably could've worded the description a lot more carefully but your comparison function according to the image worked perfectly! Only change I needed as to convert the Vectors to GameObject positions so that it works with the GameObject array. Thanks a ton! Hopefully someone else will find the other comparison function helpful.

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

130 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 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

Find a position forward of GameObject with possible angle 1 Answer

How can I move this part in a rectangle when I press a key? [C#] 1 Answer

How can I set the default position of a 3D object? 2 Answers

How do I make my sound work in proper 3D space for VR 1 Answer

Problem with positioning of scaled models 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