- Home /
RTS-Style Selection Square
I couldn't seem to find much on the topic. But I'm trying to figure out a way to program something like this
OnMouseDown()
{
//Draw Square from origin to current point
//On release select all the objects of type "whatever"
}
I have all the logic for selecting setup. But right now I can only select one at a time and would very much like to do group selecting with squares. If anyone could point me in the right direction that would be great! Thanks
Answer by AngryOldMan · Apr 26, 2011 at 07:49 PM
something like:
var lineMaterial : Material; var textureScale = 4.0; private var selectionLine : VectorLine; private var originalPos : Vector2;
function Start () { selectionLine = new VectorLine("Selection", new Vector2[5], Color.white, lineMaterial, 4.0, 0.0, 0, LineType.Continuous, Joins.Open); }
function OnGUI () { GUI.Label(Rect(10, 10, 300, 25), "Click & drag to make a selection box"); }
function Update () { if (Input.GetMouseButtonDown(0)) { originalPos = Input.mousePosition; } if (Input.GetMouseButton(0)) { Vector.MakeRectInLine (selectionLine, originalPos, Input.mousePosition); Vector.DrawLine (selectionLine); } Vector.SetTextureScale (selectionLine, textureScale, -Time.time*2.0 % 1); }
all credit to go to Eric5h5 for his superior answer to this previously asked question
Thanks. I googled Unity RTS Selection and I wish that had come up.
Answer by Jesse Anders · Apr 26, 2011 at 07:49 PM
I couldn't seem to find much on the topic.
This has actually been discussed at least a few times here and/or on the forums; I think if you search for 'rts selection', you should be able to find some useful info.
In any case, the first step is probably to decide what space you want to perform the selection in. For example, you could perform the selection in screen space using a screen-space rectangle and Camera.WorldToScreenPoint(), or you could perform the selection in world space using a picking volume.
Of those two methods, the first would probably be the easiest to get working quickly. (That's assuming there are not so many game objects in the scene that a 'brute force' approach would be unsuitable.)
Your answer
![](https://koobas.hobune.stream/wayback/20220613125513im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rts people select 2 Answers
Unity RayCast Selection 1 Answer
Click and Drag RTS Mouse selecting script help. 1 Answer
Need help with Chess Like Character Selection and Movement 1 Answer
RPC Parameter Optimization 1 Answer