Skip to content

Instantly share code, notes, and snippets.

@benloong
Created January 29, 2013 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benloong/4661195 to your computer and use it in GitHub Desktop.
Save benloong/4661195 to your computer and use it in GitHub Desktop.
RTS Volume selection
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
class Selection {
List<Unit> static PickUnitsInRect(float left, float top, float right, float bottom, Camera camera = null) {
List<Unit> pickedUnits = new List<Unit>();
if(camera == null) camera = Camera.main;
Ray bottomLeft = camera.ScreenPointToRay(new Vector3(left, bottom));
Ray topLeft = camera.ScreenPointToRay(new Vector3(left, top));
Ray topRight = camera.ScreenPointToRay(new Vector3(right, top));
Ray bottomRight = camera.ScreenPointToRay(new Vector3(right, bottom));
Plane[] planes = new Plane[5];
planes[0].Set3Points(topLeft.GetPoint(3), bottomLeft.GetPoint(3), bottomRight.GetPoint(3)); // front
planes[1].Set3Points(topLeft.origin, topRight.origin, topRight.GetPoint(100)); // top
planes[2].Set3Points(topLeft.origin, topLeft.GetPoint(100), bottomLeft.GetPoint(100)); //left
planes[3].Set3Points(bottomLeft.origin, bottomLeft.GetPoint(100), bottomRight.GetPoint(100));//bottom
planes[4].Set3Points(topRight.origin, bottomRight.GetPoint(100), topRight.GetPoint(100)); //right
foreach(Unit unit in Unit.units) {
if(GeometryUtility.TestPlanesAABB(planes,unit.bounds)) {
pickedUnits.Add(unit);
}
}
return pickedUnits;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment