- Home /
Find greatest distance between a List of objects?
I have a list of randomly instantiated objects. Using a foreach loop, I'm comparing which one has the greatest distance between them and a main object. When the comparison is made I want to know which one has the greatest distance. Problem is I don't know to go about doing this:
GameObject mainObject;
foreach (GameObject object in objects){
if (Vector3.Distance(mainObject, object){
//What should i do?
}
//Other part I don't know
}
I'm really lost with this one. I have no idea what to do. Any help would be appreciated!
Answer by T27M · May 25, 2014 at 10:33 PM
You can calculate and store each distance in a list and using the list's Sort() method, you can sort them from lowest to highest.
public List<GameObject> objects = new List<GameObject> ();
List<float> distances = new List<float> ();
// Use this for initialization
void Start ()
{
foreach (GameObject obj in objects) {
float dist = Vector3.Distance (gameObject.transform.position, obj.transform.position);
distances.Add (dist);
}
distances.Sort ();
foreach (float f in distances) {
Debug.Log (f);
}
Debug.Log ("Greatest Distance: " + distances [objects.Count - 1]);
}
Update
This does the same as above, but stores the object and it's distance from the main object in a dictionary. It then converts the Keys(distances) to a list and sorts them. Once the largest distance is determined we then simply look it up in the dictionary to find the gameobject it refers to.
using System.Collections.Generic;
using System.Linq;
...
GameObject[] _objects;
Dictionary<float, GameObject> distDic = new Dictionary<float, GameObject> ();
// Use this for initialization
void Start ()
{
// Populate array for testing purpose - use your array of objects
_objects = GameObject.FindGameObjectsWithTag ("FindMe");
foreach (GameObject obj in _objects) {
float dist = Vector3.Distance (gameObject.transform.position, obj.transform.position);
distDic.Add (dist, obj);
}
List<float> distances = distDic.Keys.ToList ();
distances.Sort ();
GameObject furthestObj = distDic [distances [distances.Count - 1]];
// Do something with furthestObj
}
Thank you! Is there anyway I could access the object with the greatest distance?
Yes, I updated the question with that part. You just take the total count of gameobjects or distances, count them and subtract 1.
Debug.Log ("Greatest Distance: " + distances [objects.Count - 1]);
Just so you know how this works; List.Count returns the actual number of elements in the List.
Say you have a list with 5 elements, List.Count will return 5. But the list is 0 index meaning it goes 0,1,2,3,4. You want the last item so List.Count -1 = 4.
Hope that helps.
if:
distances [objects.Count - 1];
returns the greatest distance then:
object[distances.Count - 1];
should return object with greatest distance. However, this seems to only return the object the was created last, not the one with greatest distance. (The objects are randomly instantiated, sorry for not including that in the question. my mistake).
Ah, sorry I skimmed over the comment/question. For some reason I thought you only wanted the distance, the method described above will give you the greatest distance only.
In response to your comment above, the reason that your code gives the last object spawned is because it will always give you the last index. There is no link between the two lists, so it does not work as you expected i.e it will always give the last index. You sorted your list so the lists no longer match up.
Do your objects move?
I figured it out!
Using System.Linq I was able to find the greatest value of the distance array without sorting it. Since the distance array is made up of the object's distances in the same order they were in the object array, all I needed to do was find the index of the greatest value of the distance array and apply it to the object array.
Thank you for your help!