Search Unity

RTS style selecter

Discussion in 'Scripting' started by Gaba, Aug 24, 2009.

  1. Gaba

    Gaba

    Joined:
    May 29, 2009
    Posts:
    41
    So, i'm struggling with algorithms here, i want to be able to select few units by dragging the mouse, or click on them and select each one, units will only take orders if they are selected.

    Now to the tricky part, the GUI would have to change if there were only one unit selected, and display the abilities of that unit

    So i was thinking:

    create a square when mouse is dragged with left button pressed, check which colliders have been hit and then send the message to them selected = true, which would be the pre-requisite to entering on all the orders methods

    but it doesn't seem to be a particularly elegant solution.

    also, i'm having trouble figuring how to change the GUI if only one guy is selected...

    So, is this really the best way? Do you guys can think of a better one?

    I'm really more interested with the whole algorithm and theories here, but codes are more than welcome

    Thanks guys
     
  2. liquidgraph

    liquidgraph

    Joined:
    Feb 26, 2009
    Posts:
    324
    I was prototyping an RTS a while back and made what you'd call a typical RTS selection set up: single-click select, rectangular marquee, and shift-click to add and subtract units. The best approach is to have an array of currently selected units to which you can add and subtract new selections. When you want to perform actions on all the selected units, like move them, you can cycle through the selected unity array and get references to each unit and update the positions accordingly.

    This structure worked very well, but it gets pretty complicated when you start getting into addition, subtraction, etc. There are many small touches that make an RTS selection setup perfect, but overall it's common sense stuff, like making sure actions register on mouse release instead of mouse down, etc. Sorry, can't post code because it might be a project we will pursue at some point.

    I used a projector to cast a selection circle around each unit in the selection array. This way the player sees all the selected units easily. The downside is that each projector uses up 1 draw call, and you'll need 1 projector per unit, which can become a performance drag if you're talking about a serious RTS with 100+ units on screen. Projectors work nicely with terrain because they just cast over-top irregular surfaces (mountains, hills, bumps, even other meshes).

    Use raycasts to determine if the player clicked on a unit. For the selection marquee, you will need to loop through every unit on screen and check if its x, y, z coordinates are within bounds of the player-traced rectangle, which can be defined by simple functions such as x=2, x=5, z=0, z=10. If a unit's position is within these bounds, add it to the selection array. I found it very helpful to set up separate functions that:

    1. Add to the selection array
    2. Remove from the selection array
    3. Find an individual unit within the selection array
    4. Deselect an individual unit from the array
    5. Clear the entire array

    You'll want to take an OO approach because like I said, it gets a bit hairy.
     
  3. Gaba

    Gaba

    Joined:
    May 29, 2009
    Posts:
    41
    i'm cool with projector, the game is not an RTS, but more of a diablo style game with multiple units, but the RTS control scheme makes sense for the game

    about what you said, sounds like a good idea, and i can even make it so if the array size is 1, i can draw the GUI according to the tag that unit have
     
  4. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058