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
1
Question by youngapprentice · May 05, 2013 at 05:43 PM · proximity

Order Objects Based on Proximity

Hi, all!

I am using Physics.OverlapSphere to get an array of all colliders in a certain range.

Currently in my script, it picks a random object. However, I would like to order these objects in the array based on proximity to the gameObject.

How would I go about this?

Thanks!- YA

Comment
Add comment · Show 4
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 AlucardJay · May 06, 2013 at 12:23 AM 0
Share

You could check my answer here for a bubble-sort technique : http://answers.unity3d.com/questions/246781/sort-transforms-by-distance-to-player.html

avatar image youngapprentice · May 06, 2013 at 01:10 AM 0
Share

Your solution seems to be more practical than $$anonymous$$e, but I just figured it out. Thanks, though! This will be valuable later!

avatar image Eric5h5 · May 06, 2013 at 02:29 AM 0
Share

Since .NET has sorting built-in, you don't usually need to write your own sorting code, you just need a function that returns -1, 0, or 1 based on some criteria, so the sort can work based on whatever you need.

avatar image youngapprentice · May 06, 2013 at 02:43 AM 0
Share

Ah. Thank you for the advice. This will be good to look into.

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Eric5h5 · May 06, 2013 at 02:27 AM

You can use an inline function to sort an array:

 var possibleTargets = Physics.OverlapSphere (transform.position, 100);
 System.Array.Sort (possibleTargets,
     function (a : Collider, b : Collider)
         (transform.position - a.transform.position).sqrMagnitude.
         CompareTo ((transform.position - b.transform.position).sqrMagnitude));

You can also use an external function, which may be more readable, since it's not all on one line:

 System.Array.Sort (possibleTargets, DistanceSort);

 function DistanceSort (a : Collider, b : Collider) : int {
     return (transform.position - a.transform.position).sqrMagnitude.
         CompareTo ((transform.position - b.transform.position).sqrMagnitude);
 }
Comment
Add comment · Show 8 · 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 youngapprentice · May 06, 2013 at 02:43 AM 0
Share

But what would be the next step? Add a switch case to populate the array before or after an element?

avatar image Eric5h5 · May 06, 2013 at 03:05 AM 0
Share

If you want the array based on some criteria, you'd set it up first (probably using a List), then sort it. You can use List.Sort in pretty much the same way as Array.Sort.

avatar image youngapprentice · May 06, 2013 at 03:42 AM 0
Share

But I am trying to sort transforms by indexing them with their respective distances from the player at a given time.

That makes 1 Transform, float pair per object. Sort by float, retrieve transform.

This looks to me like you are just sorting a list(?)

avatar image Eric5h5 · May 06, 2013 at 04:50 AM 0
Share

A list of transforms, yes.

avatar image youngapprentice · May 06, 2013 at 11:06 AM 0
Share

Oh I see. You are supplying Array.Sort with the parameters of the Transforms and a function to use to decide on where they go, because your external function returns an int.

Show more comments
avatar image
0

Answer by Bluk · May 05, 2013 at 05:52 PM

-Construct an array (or two, or associative, or hashtable) of distances between the gameObject and the colliders (http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Distance.html )

-Sort the array

That's pretty much it

Comment
Add comment · Show 5 · 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 Julien-Lynge · May 05, 2013 at 06:01 PM 0
Share

To get the distance with @Bluk's method, do the square magnitude distance, rather than the magnitude distance. It won't change the order of the objects in the array, and it's faster ((a-b).sqr$$anonymous$$agnitude)

However, depending on how many objects you have, this method may or may not work. If you have fewer than a thousand objects you should be fine; if not, there are other methods that are faster but harder to implement.

avatar image youngapprentice · May 05, 2013 at 08:01 PM 0
Share

I know how to create the array of distances and sort them. But once that is done, I will have an array of floats. How do I get the array of objects to line up with the array of corresponding floats?

I am not new to using hastables but am new to coding them and making use of them. I am using UnityScript as of right now.

avatar image youngapprentice · May 05, 2013 at 09:24 PM 0
Share

$$anonymous$$y question is about the sorting step. I would be very pleased if you could expound upon what you mean.

avatar image youngapprentice · May 05, 2013 at 09:44 PM 0
Share

So I have dynamically populated a dictionary with the key being the object and the value being the distance from player. How do I sort the dictionary by the value per key?

avatar image youngapprentice · May 06, 2013 at 01:11 AM 0
Share

In case anyone is interested, here is my finished code:

         var myRocketCount : int = ship.activeGun.unique.Value;
             var myTargets = new Dictionary.<float, Transform>();
             var myDistances : float[] = new float[myRocketCount];
             var possibleTargets : Collider[] = Physics.OverlapSphere( Vector3( aim.position.x, aim.position.y, GameObject.Find( "EnemyGrid" ).transform.position.z), 30 );
             if( possibleTargets.length >= myRocketCount ){
                 for( var i = 0; i < myRocketCount; i++ ){
                     if(StaticFunctions.HasProperty( possibleTargets[i].transform, "enemy" )){
                         myTargets.Add( Vector3.Distance(possibleTargets[i].transform.position, aim.position), possibleTargets[i].transform);
                         myDistances[i]= Vector3.Distance(possibleTargets[i].transform.position, aim.position);
                     }
                 }
                 System.Array.Sort( myDistances );
                 for( var w = 0; w < myDistances.length; w ++){
                     if(myRocketCount > 0 && ship.activeGun.plasma.current >= ship.activeGun.plasmaCost.Value){
                         var myRocket : Transform = Instantiate( ship.activeGun.bullet, Vector3( GunC.position.x,GunC.position.y-3, GunC.position.z + 2), GunC.rotation);
                         myRocket.GetComponent( RocketAim ).target = myTargets[ myDistances[w] ];
                         myRocketCount --;
                         ship.activeGun.plasma.current -= ship.activeGun.plasmaCost.Value;
                     }
                 }
             }
avatar image
0

Answer by Griffo · May 05, 2013 at 06:12 PM

Place this on an empty game object and it should give you every object in the blastRadius in order of closest first, needs altering to suit your requirements, amount, distance .. ect.

 #pragma strict
 
 var myArray : String[] = new String[200];
 var blastRadius : float = 100;
 
 private var i : int;
 
 function Start () {
 
     AreaDamageEnemies(transform.position, blastRadius);
 }
 
 function Update () {
 
 }
 
 function AreaDamageEnemies(location : Vector3, radius : int){
 
     var objectsInRange : Collider[] = Physics.OverlapSphere(location, radius);
                     
     for (var col : Collider in objectsInRange){
 
         while(i < objectsInRange.length){            // Go through the objectsInRange array (objects in the Physics.OverlapSphere radius)
             myArray[i] = objectsInRange[i].name;    // Add each GameObject.name to myArray
             print(objectsInRange[i].name);
             i++;
         yield;
         }
     }
 }
Comment
Add comment · Show 2 · 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 youngapprentice · May 05, 2013 at 06:40 PM 0
Share

@Griffo- this would lead me to believe that OverLapSphere already returns an array of closest objects first.

avatar image Eric5h5 · May 06, 2013 at 02:29 AM 0
Share

It doesn't; the order is undefined.

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

17 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

Related Questions

Help with Array script 1 Answer

Fading GUI text 2 Answers

Mesh Inertia Tensor? 1 Answer

Learning process 1 Answer

What type of sign do I use for a Scene? 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