noobtuts

RTS Selection System

Unity RTS Selection System
Our RTS Selection System for Unity is the easiest way to enable single click- and rectangle selection in Real Time Strategy (RTS) games.

Unity Webplayer Demo: click here

How it works

The RTS Selection System allows the player to do click- and rectangle selection that is common in the RTS genre. It then calls the OnSelect and OnDeselect functions in each (de)selected entity.

Usage Guide

Select the Main Camera:
Main Camera in Hierarchy

Add the Script by selecting Add Component->Scripts->RTS Selection System in the Inspector:
Camera with RTS Selection System Script
Note: the Mousebutton property lets us choose between left mouse button (0), right mouse button (1) and middle mouse button (2). The Check Visibility option can be enabled if only want to select objects that are visibile to the Camera (not behind a wall). The Skin property allows us to change the looks of the selection rectangle by using a GUI Skin and then modifying the box style.

Press Play, hold and drag the mouse button across the Screen to see the selection rectangle:
Selection Rectangle

We will create a little selection circle for our test scene and add it to each unit:
Selectioncircle

We then disable the circle, so that it's invisible by default.

Afterwards we can add a Unit Script to each unit that takes care of enabling and disabling the circle by using OnSelect and OnDeselect:

using UnityEngine;
using System.Collections;

public class Unit : MonoBehaviour {
    // Selection Circle
    public GameObject circle;
   
    void OnSelect() {
        circle.SetActive(true);
    }
   
    void OnDeselect() {
        circle.SetActive(false);
    }
}

Now we can save the Script and drag each unit's Selection Circle into the Script's Circle slot:
Unit Script with Selection Circle

If we press Play again then we can now select and deselect units:
Selection

Advanced Information

Please note that this Editor Extension is intended for 3D games only at the momemt. Furthermore, the OnSelect and OnDeselect functions will only be called on GameObject's that have a collider attached to them.

The Script can automatically do visibility checks, so that OnSelect will only be called for Units that are actually visible (as in: not behind a wall).

The Extension also takes perfectly good care of single click selection. Simply use the OnSelect function for single click and rectangle selection, there is no need to use OnMouseDown or Update for selection logic.

Example Game

Our Unity RTS Game Tutorial uses the RTS Selection System to create a RTS game, so feel free to check it out too.

Download

Our RTS Selection System is included in the Premium files.


Download Source Code & Project Files

The RTS Selection System source code & project files can be downloaded by Premium members.

All Tutorials. All Source Codes & Project Files. One time Payment.
Get Premium today!