Handling RTS unit selection

I am working at my project, and just noticing of how laggy things are sometimes, so i’d like to know a couple of things:

is it faster to add EVERY single unit in the game (may be from 2 to 200) when it’s spawned to an array and when i click/drag check the position of every single object in the array, and if it equals the click position, then tell it that it has been selected,
or just to make a raycast?

also, is there another way other than projector to create a "white zone2 'round an unit like classic rts games when something is selected? i have just heard that it force the terrain to be drawn twice!
Thanks for any help

Phil

When you do a raycast, the engine must loop through every collider - so there’s no help from raycasting there.

You could probably get away with some other way to draw the selection circle if you’re clever. You could do a simple flat plane, or a smarter (but slightly slower) way could be to use a LineRenderer whose points conform to the terrain. Both methods are still much preferred to a projector, though - you’re exactly right that a projector would could the terrain to be rendered again for every projector.

Philip, I recommend judicious use of the function OnMouseOver. Basically, if the mouse is overhead and the Input.GetMouseButtonDown(0) == true, then you’ve selected that unit. You just need to give them a big enough collider to be clicked on.

Selecting groups of units is a little different. I created a trigger box that sends all non-terrain objects inside a message to tell my controller script that they’ve been selected. This box is determined by the starting point of the click and the ending point of the click. Basically, the position on the terrain between those points is the center of the trigger box and the width and length are determined by the difference between the starting and ending position on the x and z axis. (I got those positions by raycasting at the start and finish) Of course, the height of the collider needs to be something like 300 if you’ve got really big mountains, but that’s no big deal.

I’m using a simplistic approach and use 2 raycasts to determine the selection rectangle on a normalized ground area and do then a point-in-rect test to see if the units are in. Not perfect, but works fast (I think). Download it here:

http://forum.unity3d.com/viewtopic.php?t=18802