x


How would i make this code not lag the game

 void GetNearestMinion()
    {
        foreach (GameObject Minion in EnemyMinions)
        {
            float finishTime = Time.time + waitTime;
            MinionsTransforms.Add(Minion.transform);

        }
        MinionsTransforms.Sort(delegate(Transform t1, Transform t2) { return (Vector3.Distance(t1.position, t2.position).CompareTo(Vector3.Distance(t2.position, transform.position))); });

        nearestMinion = MinionsTransforms[0].gameObject;
    }

How can i stop this from lagging the game keep in mind that every minion does this check at least once every frame(which i don't mind reducing)?

more ▼

asked Jul 03 '12 at 03:46 PM

konsowa gravatar image

konsowa
16 2 3

So you have every minion getting every other minion's transform, and on top of that sorting it? Not sure you can make that not lag your game. What are you trying to achieve here? Maybe we can find a different way of achieving the same behavior.

Jul 03 '12 at 04:17 PM ThePunisher

If you are searching for the nearest minion, why not just use Vector2(or .3).Distance?

Jul 03 '12 at 04:50 PM Luci85

trying to sort minions by distance and get the first one in the array

Jul 03 '12 at 04:56 PM konsowa

btw, how often do you call this function?

Jul 03 '12 at 05:04 PM Bunny83

From what it sounds Bunny83, it seems like he calls that for every unity every frame so I can see why it lags.

Jul 03 '12 at 05:58 PM ThePunisher
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

There are several things not very efficient here:

  • Sorting the whole array is totally unnecessary. You just want the closest.
  • You should hold an array with the transform references so you don't need to get each transform everytime.
  • Just do a simple min distance search that compares the squared distance. This is much faster than calculating the true distance. The relations stay the same.

Something like that:

Transform GetNearestMinion()
{
    Transform nearestMinion = null;
    Vector3 myPos = transform.position;
    float minDist = 999999;
    foreach (Transform Minion in EnemyMinions) // EnemyMinions is now a Transform array or List
    {
        float dist = (Minion.position - myPos).sqrMagnitude;
        if (dist < minDist)
        {
            minDist = dist;
            nearestMinion = Minion;
        }
    }
    return nearestMinion;
}

This is the most optimised way. ps, i've written that from scratch, so there might be typing mistakes ;)

This function now returns the nearest Minion or null if there is none at all.

more ▼

answered Jul 03 '12 at 05:03 PM

Bunny83 gravatar image

Bunny83
45.1k 11 48 206

If i have 10 minions doing this at the same time every frame that would definitely make the game lag rite?

Jul 03 '12 at 05:16 PM konsowa

Oh and the minions move which is y i need to get the transforms a lot, think of League of Legends when answering and how that game works

Jul 03 '12 at 05:17 PM konsowa

Well, if you have just 10, your code shouldn't be that slow. However, Unity, or LINQ uses

Quicksort so it get's quickly worse. Your method needs in the worst case 100 comparisions for 10 elements. If you have 40 elements it would be 1600 comparisions. Each of your comparisions calulates the distance two times. The distance calculation requires a square-root, which is almost the slowest arithmetic operation that exist (besides Pow).

My function doesn't use any square root, just float multiplications / additions and you only have to check each minion once since you just need the nearest and not the whole array sorted...

Btw, does every minion execute this, or just you, the player? If it's executed for every minion that would really be crazy... That's x^3 in the worst case. For 10 minions that would be 1,000 comparisons per frame... and for 100 minions that would be 1,000,000

Jul 03 '12 at 05:27 PM Bunny83

Well the thing is i have 5 spawning for each team every 15 seconds which means there will be more than 10 And Yes every minion does it..The thing is im not too woried about calculations what im worried about is the foreach loop isnt that slowing things up?

Did you mean sqrMagnitude in that code up there instead of sqrDistance?

Jul 03 '12 at 05:41 PM konsowa

A foreach loop per se doesn't make things slow, it entirely depends what happens in the loop. @Bunny83's approach is exactly right. While you actually sorted the whole array, which is extremely expensive but totally irrelevant, he just loops through the transforms and finds the closest one. He uses sqrMagnitude (yes, there is no sqrDistance), which does not use an expensive Mathf.Sqrt(), and the simple loop with an O(n) is much faster especially for a large number of elements, than a quicksort algorithm, which needs at least O(n * log n) to sort the array.

Jul 03 '12 at 06:07 PM Wolfram
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x531
x294
x196

asked: Jul 03 '12 at 03:46 PM

Seen: 471 times

Last Updated: Jul 03 '12 at 11:57 PM