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)?
(comments are locked)
|
|
There are several things not very efficient here:
Something like that: 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. 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
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)
|

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.
If you are searching for the nearest minion, why not just use Vector2(or .3).Distance?
trying to sort minions by distance and get the first one in the array
btw, how often do you call this function?
From what it sounds Bunny83, it seems like he calls that for every unity every frame so I can see why it lags.