How can I do this and how can I select multiple characters in my game by drawing a guy box over them (like in every RTStrategy game)? :?:
Off the top of my head, you could use Camera.ScreenToWorldPoint() to get the coordinates for your expanding box container, then use Physics.OverlapSphere() to return all the objects inside. I didn’t see Physics.OverlapBox(), but I doubt anyone would notice.
I seem to remember a couple people working on RTS games, Im sure the topic has come up before.
Okay, maybe this is a bit difficulter than I thought… :?
I tried this, but it isn’t working:
private var x1 = Input.mousePosition.x;
private var y1 = Input.mousePosition.y;
private var x2 = Input.mousePosition.x;
private var y2 = Input.mousePosition.y;
function OnGUI () {
var startX = 0.0;
var startY = 0.0;
var endX = 0.0;
var endY = 0.0;
if (Input.GetMouseButtonDown(0)) {
startX = x1;
startY = y1;
}
if (Input.GetMouseButton(0)) {
endX = x2;
endY = y2;
}
GUI.Box(Rect(Screen.width * startX,Screen.height * startY, endX, endY), "Selection");
}
Does anybody who created an RTS-game has coded a selection tool? I don’t get it how to script it correctly, so that a GUI-box is drawn if you hold down mouse and the characters inside this area are selected. :?:
i have the same problem and i found that the issue is the drawing of the rectangle if anyone know of an easy way to draw rectangle this would be handy and drawtexture with stretching doesnt work either
Vectrosity is a good way for drawing selection boxes, and without the fill-rate hit of stretching a texture over a large area of the screen (plus you can do interesting animation effects). See the interactive example partway down the page.
–Eric
@KlaRo, your code isn’t working because you’re only sampling the mouse position once when the script is loaded. You need to be reading the mouse position each time in OnGUI(), using Event.current.mousePosition, since otherwise you’ll never have up-to-date mouse coordinates. If you choose to pursue the GUI.Box() approach rather than switch to Eric’s awesome Vectrosity tool, that’s the only immediate problem I saw in your code. Well, that plus the fact you don’t have any code to actually select the things the box contains once it’s draw properly, of course