How to selected objects drawing a box like in rts games
I know how to draw box, but i dont know how to send message to selected object. I want to do a rts strategic game and i know how do the other things but that is problem to me, please help and thanks. :)
Answer by Statement · Oct 21, 2015 at 01:52 PM
There is no API in Physics to allow you to test vs a frustum so what you can do is convert object locations to screen space and do the box test there. If the object is inside the box, send your message to it.
Here's psuedocode for it.
Use Camera.WorldToScreenPoint to convert from world to screen space.
for each object
point = WorldToScreen (object)
if box.Contains (point)
object.Select ()
If you get annoyed with just testing a point, try and work with their renderers bounds.
It'll feel a lot more responsive if you can select a unit by partially covering the visible area, rather than having to find its transforms position.
To convert the renderers bounds to a rect, you need to convert all eight corners of the box to screen space. I've made a utility class that you can use to test for rect overlapping and converting bounds to rect.
To use it, imagine the following code, but with proper sources for selection
and all
.
void Example()
{
Rect selection = new Rect(0, 0, 123, 123);
GameObject[] all = new GameObject[123];
foreach (var go in all)
{
if (SelectionUtil.Overlaps(selection, go))
{
// Do something with go.
go.SendMessage("Selected");
}
}
}
I also include a complete example project that you can download from github.
Your answer
![](https://koobas.hobune.stream/wayback/20220612042146im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Selecting one instantiate prefab 1 Answer
Having Trouble With Turn Base Strategy Grid 0 Answers
Animate RTS building with worker 0 Answers
RTS Click Drag Selection box not working with GUI canvas 1 Answer