I need something like
//Vector3 nearest = PlayerUnits.Sort(delegate(Transform t) { return (transform.position - t.position).sqrMagnitude; }).First();
to exist…
to where it sorts, finds the first answer, and then stops sorting how is this possible? the only way to sort that i know of in c# is:
private IEnumerator DistanceToTarget()
{
//Sort so nearest enemy is always Enemies[0]
PlayerUnits.Sort(delegate( Transform t1, Transform t2){
return Vector3.Distance(t1.position,transform.position).CompareTo(Vector3.Distance(t2.position,transform.position));
});
yield return new WaitForSeconds(1f);
AlreadyRunning = false;
}
Which is horrible on performance for many cases i need a better method. The reason for this is i need to be able to have about 1000x1000 for this compareto that is constantly being done. There has to be a way…supreme commander is a good example it can handle 1000’s of units in realtime.
I’m pretty sure that the implementations of LINQ that’s in Unity’s (very old) Mono implementation had some serious drawbacks. Also, even with the fastest search algorithms, 1000 units looking at the closest of 1000 other units in real time is just not viable; you’ll have to come up with a smarter solution.
You dont need to compare 1000 to 1000 others, you only need a single run through of all 1000. You can also discard magnitude in favor of sqrMagnitude to save on a sqrRoot calculation