how about the logic itself?
does every unit call this method, and check against all other units in the for loop? i.e. n * (n-1)
if so, then consider the middle for loop for 3 Units ABC
A against A (pointless)
A against B
A against C
B against A (redudant)
B against B (pointless)
B against C
C against A (redudant)
C against B (redudant)
C against C (pointless)
instead only calculate those values once per frame
SomeValue[][] someValueMatrix;
GenerateValueMatrix() {
Unit[] units = // list at UnitController.GetInstance().UnitsInGame
for(int cursor = 0; cursor < units.Length; cursor++) {
Unit unitA = units[cursor];
someValueMatrix[cursor, cursor] = -1; // ignore unit to itself
for(int unitBIndex = cursor + 1; unitBIndex < Units.Length; unitBIndex++) {
Unit unitB = units[unitBIndex];
var d = SomethingSomething(unitA, unitB) // the inner part of the for loop calculation, so calculating the distance or dir or w/e
someValueMatrix[cursor, unitBIndex] = someValueMatrix[unitBIndex, cursor] = d; // relationship from a to b is same as b to a
}
}
}
then in the UpdatePresenter method you can do the for loop like
Unit[] units = UnitController.GetInstance().UnitsInGame.ToArray();
int myIndex = // units own index in the array
// Loop through all units
for (int i = 0; i < units.Length; i++)
{
if(myIndex == i) continue;
dir += someValueMatrix[myIndex, i]; // add the vector or w/e you have stored in the matrix at this point
}
dir *= .1f;
note1, before a moved in response to b, and b moved in response to the updated position of a – now the response is static based on initial a-b position difference per frame
note2, optionally you can check if a unit has moved at all, so if neither a nor b has moved, there is no need to update their relationship value
note3, if you add/remove a unit you have to recreate the matrix accordingly, otherwise just re-initialize it per frame