- Home /
Sorting Collider Arrays by location of elements
Hello everyone,
I am trying to sort an Array of Colliders which I get the following way:
Collider[] hitColliders = Physics.OverlapSphere(l_handBase.transform.localPosition, 2.0f, 9);
This returns me colliders, from which I then need to sort the x position of the parent of the transform of the collider.
Explanation(not actually valid code):
for (int i = 0; i < hitColliders.Length; i++){
hitColliders[i].transform.parent.localPosition.x
}
I need to sort the colliders by the x values of the localposition, I don't think I can just use a float List, since I wouldn't be able to access the colliders directly anymore.
Any help would be greatly appreciated!
Answer by Estevominador · Oct 24, 2018 at 12:31 AM
Hey, dude... I literally just got this ^^
You must use this on your code: using System.Linq;
then use: Collider[] hitColliders = Physics.OverlapSphere(l_handBase.transform.localPosition, 2.0f, 9);
hitColliders.OrderBy (x => Vector3.Distance (this.transform.position,x.transform.position)).ToArray ();
Your answer
Follow this Question
Related Questions
Vector3 resultant array sorting 2 Answers
MergeSort function acting strange 0 Answers
How to sort get components? 3 Answers
Scoring System ArgumentOutOfRangeException Error 2 Answers
Multiple Cars not working 1 Answer