RTS Selection system (Arrays versus Physics)

Hey guys,
I’m making a small stratergy game and am therefore in need of a decent group selection system. Currently, I have implimented the code written by Martin Schultz for his Generals project back in 2009. The code works fine however it is not very scalable:(.

This method is passed the mousedown and mouseup positions after a mouse drag action.

This code works, however I have noticed that once the group selection gets above 10 units or so, I take a noticable frame rate hit everytime I call this. I have tried changing the arrays to built in arrays as I realise these are much faster but oddly that seemed to be of little benefit. I was expecting at least a modest gain in speed over using Java Arrays. Now my thinking is that it is not the iteration itself that is causing the slowdown but the hardcoded checks on unit position.

My other line of enquiry was to use a scable collider at run-time and simply get the objects within its bounds. I’m guessing this makes use of the Mesh class and transform.InverseTransformPoint inorder to translate my dragging mouseposition (which I already have in worldspace elsewhere) to localScale. I’ve yet to get this to work but I am sure it must be possible? I’m not the best coder in the world as you may have guessed, hopefully not the worst either. I’d really appreicate a nudge in the right direction from somebody.

Thanks in advance:smile:.

Have you tested if you get a frame hit when you are not running in debugMode?
Also you could try to have a seperate array that only contains Vector2 of allUnitsList and make sure that the indexes matches.
So everytime you spawn or destroy an object you change both arrays in the same way.
So visual you have
allUnutsList = [GameObject,GameObject,GameObject] // and
allUnitsVectorList = [Vector2, Vector2, Vector2].

So when you spawn a GameObject you also push it on the allUnitsVectorList by:
allUnitsList.Push( newGameObject )
allUnitsVectorList.Push( newGameObject.transform.position)

Then you work with the allUnitsVectorList in the code you posted, instead of allUnitsList.

I’ll try turning off the debug mode but as it’s just printing to the console I can’t imagine it’s that bad. I should only be using Vector2 I agree on that as it’s a 2D game. The problem is my units move so there positions are changing so any Vector2 array would quickly be out of date. Maybe I’m looking at this the wrong way though and there is a means to get my units to report their positions during a selection event.