- Home /
Question by
Uriel_96 · Apr 11, 2011 at 02:19 AM ·
javascriptvector3distance
Get 4 smallest distance
OK, I already know how to find the smallest distance:
for(var i = 0;i<nodes.Length;i++){
var actualdistance = Vector3.Distance(enemy.position,nodes[i]);
if(i == 0){
mindistance = actualdistance;
}
if(actualdistance < mindistance){
mindistance = actualdistance;
start = i;
}
}
but Know what I am trying to figure it out is how to make this but not only with the smallest distance, get lets say the 4 smallest distance so my question is, how can I do this?
Comment
Best Answer
Answer by loopyllama · Apr 11, 2011 at 06:52 AM
If you only want the 4 smallest distances as you asked, fill an array with the distances, do an Array.Sort() then grab the first 4 elements of that array.
Here are the docs for Array:
http://unity3d.com/support/documentation/ScriptReference/Array.html
O$$anonymous$$, but what about if I only want to know the number of the arrays that are the smallest not actually order the list
Your answer
![](https://koobas.hobune.stream/wayback/20220613121954im_/https://answers.unity.com/themes/thub/images/avi.jpg)