RTS Unit Selection in Unity

Real-time strategy games usually allow a user to click and drag a box to select all the units within that box. A single left click would clear the selection. I have not found a tutorial that uses the UI canvas for this, so I took it upon myself to implement a solution and share it.

Setting Up The Scene

A unit–it could be a box– is needed to be the thing that a user selects, then a plane is needed for our ground, and finally a canvas element.

Setting Up The Unit

Your box should look something like this

  • Create a box game object
  • Attach an empty child game object– this will be our halo
  • Add a projector component to it
  • Add a collider component to ensure raycasts will hit it

Your halo should look like so

The projector component has to ignore all layers except the ground layer. Or else the projector will project the halo on top of other units / objects.

Position the halo like so on the unit

Download this and use it as the texture

Download Selection Shader

Full credit where credit is due, I took the halo and some code from this blog post.

Setting Up The UI Canvas

Create a canvas element and then attach an image component, this will be our select box.

It should look the this

Code Pieces

  • SelectionController - Manages which entities are within a box.
  • UiController - Renders the selection box on screen.
  • SelectUtil - Contains core logic for finding entities within a box.

I wrote about my game architecture in an other post. I recommend you read that to understand how these controllers connect with each other.

Creating Selection Controller

public class SelectionController {

  private Level level;
  private Team playerTeam;

  public List<Entity> selection { get; private set; }

  private Vector3 anchor;
  private Vector3 outer;
  private bool hasActiveBox;

  public SelectionController(Level level){
    this.level = level;
    selection = new List<Entity>();
    this.playerTeam = level.playerTeam;

  public void ClearSelection() {

    EventManager.Instance.TriggerEvent(new SelectionEvent(selection));

  private void AddToSelection(Entity entity) {
    DebugUtil.Assert(entity != null, "Selected object with no entity");



  private void AddAllWithinBounds() {
    Bounds bounds = SelectUtils.GetViewportBounds(Camera.main, anchor, outer);

    this.level.playerTeam.ForEach( (Entity entity) => {
      if (SelectUtils.IsWithinBounds(Camera.main, bounds, entity.transform.position)) {

  private void AddSingleEntity() {
    Entity entity = SelectUtils.FindEntityAt(Camera.main, anchor);

    if (entity != null) {
      if (entity.team == level.playerTeam) {

  private void RemoveAllSelections() {

    foreach (Entity entity in selection) {


  public void SelectEntities() {

    if (outer == anchor) {
    } else {

    hasActiveBox = false;

    EventManager.Instance.TriggerEvent(new SelectionEvent(selection));

  public void CreateBoxSelection() {
    hasActiveBox = true;

    anchor = Input.mousePosition;
    outer = Input.mousePosition;

    EventManager.Instance.TriggerEvent(new StartSelectBoxEvent(anchor));

  public void DragBoxSelection() {
    if (hasActiveBox) {
      outer = Input.mousePosition;

      EventManager.Instance.TriggerEvent(new DragSelectBoxEvent(outer));


It is used like so:

void Update() {
  if (Input.GetButtonDown("Fire1")) {

  if (Input.GetButtonUp("Fire1")) {


When the left-click button is first pressed the initial box position is placed. As the mouse moves the DragBoxSelection will update the boxes size and trigger events for the UiController to render on screen.

private void OnDragSelectBox(DragSelectBoxEvent e) {

Once a selection has been made a SelectionEvent is triggered, which clears the box and can then be used by an other controller.

Selection Helper

public class SelectUtil {

  public static Entity FindEntityAt(Camera camera, Vector3 position) {
    RaycastHit hit;
    LayerMask mask = 1 << LayerMask.NameToLayer("Entity");

    if (Physics.Raycast(camera.ScreenPointToRay(position), out hit, 100, mask.value)) {
      Entity entity = hit.transform.gameObject.GetComponent<Entity>();
      if (entity != null) {
          return entity;

    return null;

  public static Bounds GetViewportBounds(Camera camera, Vector3 anchor, Vector3 outer) {
    Vector3 anchorView = camera.ScreenToViewportPoint(anchor);
    Vector3 outerView = camera.ScreenToViewportPoint(outer);
    Vector3 min = Vector3.Min(anchorView, outerView);
    Vector3 max = Vector3.Max(anchorView, outerView);

    min.z = camera.nearClipPlane;
    max.z = camera.farClipPlane;

    Bounds bounds = new Bounds();
    bounds.SetMinMax(min, max);
    return bounds;

  public static bool IsWithinBounds(Camera camera, Bounds viewportBounds, Vector3 position) {
    Vector3 viewportPoint = camera.WorldToViewportPoint(position);

    return viewportBounds.Contains(viewportPoint);


I like to break out core logic like this into its own utility, for unit testing and reusability. This can be used for other features, like figuring out which units are within an attack range.

Creating UI Controller

public class UiController {

  GameObject selectboxObject;
  GameObject canvas;
  RectTransform rectTransform;
  Vector2 anchor;

  public UiController() {
    selectboxObject = GameObject.Find("/HUDCanvas/Selectbox");
    rectTransform = selectboxObject.GetComponent<RectTransform>();
    canvas = GameObject.Find("/HUDCanvas");


  public void StartSelectBox(Vector3 anchor) {

    this.anchor = ScreenToLocal(anchor);

    rectTransform.localPosition = new Vector3(this.anchor.x, this.anchor.y, rectTransform.localPosition.z);

  public void DragSelectBox(Vector3 outer) {
    Vector2 rectPos = ScreenToLocal(outer);
    Vector2 newSize = new Vector2();
    Vector3 newPosition = new Vector3(anchor.x, anchor.y, rectTransform.localPosition.z);

    if (rectPos.x - anchor.x < 0) {
      // move location
      newSize.x = anchor.x - rectPos.x;
      newPosition.x = rectPos.x;
    } else {
      newSize.x = rectPos.x - anchor.x;

    if (rectPos.y - anchor.y < 0) {
      // move location
      newSize.y = anchor.y - rectPos.y;
      newPosition.y = rectPos.y;
    } else {
      newSize.y = rectPos.y - anchor.y;

    rectTransform.sizeDelta = newSize;
    rectTransform.localPosition = newPosition;

  private Vector2 ScreenToLocal(Vector3 point) {
    Vector2 pos;
    RectTransformUtility.ScreenPointToLocalPointInRectangle( canvas.transform as RectTransform, point, Camera.main, out pos);

    return pos;

  public void ClearAll() {

  public void ClearBox() {
    rectTransform.sizeDelta = new Vector2(0, 0);


The final piece is the UiController this actaully renders the image on screen to see which units will be selected. Remember it’s best practice to always separate view from logic, for reusability and testing. The RectTransform was used to convert mouse clicks into a canvas position, so the box would appear in the correct place.

I’ll be continuing the series next with commanding entities to move.

Related Posts

Test Your Chinese Using This Quiz

Using Sidekiq Iteration and Unique Jobs

Using Radicale with Gnome Calendar

Why I Regret Switching from Jekyll to Middleman for My Blog

Pick Random Item Based on Probability

Quickest Way to Incorporate in Ontario

Creating Chinese Study Decks

Generating Better Random Numbers

Image Magick Tricks

My Game Dev Process