- Home /
Create selection box by double tapping then dragging? (android mobile)(cnc style)(image embedded)
I am curious how to make a box (visually show selection while dragging) to select units to give orders to, like command and conquer does?
Like this game uses:
I also would prefer fill, as it is user friendly to see the insides of the selection box.
I supposes i could do raycast draw, but i know it could be used to represent the box, but how do i find each object inside it? Make a rectangle and use the "contains method"? How do i account for the camera being at an odd angle or rotation? Would i be able to store all objects inside an array of gameobjects, then iterate through them? How would i do the fill, even if my camera is at odd angles?
I don't know the best way to do this. Can anybody give me examples or steer me in the right direction?
I did find this post: selectionbox example
But it didnt seem to really help me.
Answer by 1337GameDev · Aug 26, 2013 at 02:25 AM
I did eventually figure this out. I just created a touch handler script to detect the states of the selection box: start, active, reset, selectUnitsWithSelectionBox.
I used a semi transparent guiTexture for the selection box, and kept track of the starting point, and then stretched the box based on the current touch and the starting point.
Then i handled moving the box if i cancelled the selection, or if i selected objects.
Then for object selection, i would report the positions of all gameobjects that are being rendered (using a list created by the isRendered flag), and select the ones that have screen coordinates in the guiTexture.
I used:
if(selectionBoxRectangle.Contains( MainActiveCamera.WorldToScreenPoint(currentObjToTest.transform.position) ))
This seemed to work fine.
For selection, I would just create a prefab that has a plane that billboards to the active camera, and is scaled to be large enough to surround the largest bounds of the shop. I did this using bounds.Encapsulate, and got the largest bounds from the center of the unit.
The prefabs also displays contextual data, such as health bars, shield, and armor of the prefab's parent.
Hope this will help somebody in the future who stumbles across this post.