A while back a user gave me this code for writing an RTS for selection. At the time I had no idea what I was doing in Unity, and frankly took off more than I could chew, and gave up on that project . Instead I made this: http://forum.unity3d.com//viewtopic.php?t=14160 (comments are greatly appreciated, btw). But now I think I’m ready to go back. Here’s the post http://forum.unity3d.com//viewtopic.php?t=13225&highlight=strategy. And the code I’m having trouble with is var unitScreenPos : Vector3 = Camera.main.WorldToScreenPoint(unit.transform.position);
I can’t figure out if thats from the camera’s point of view, and how you could use that for selection without looping through all your units over and over again. I dunno. I thought I’d post it.
That tells you the screen pixels of where the object is located. Or, to be exact, the location of the object’s pivot point. To make use of that information, for single-clicks, you could use Input.mousePosition and see if the x and y coords of unitScreenPos are similar to the x and y coords of the mouse position. For drag selecting, make a rect out of the mouse position where the user first clicked and the current mouse position, and then use Rect.Contains() on all of the on-screen objects to see if they’re within the selection or not. And yeah, you’d loop through all objects every frame during the drag-select process–this isn’t a big deal unless you have millions of objects, in which case selection speed is the least of your problems. ![]()
–Eric
One of the things newbies – myself included way back when – would really benefit from is a little one page primer on how to convert between different coordinate systems.
Here’s my first stab at it.
Shameless plug for my own RTS engine, you say? I’d love to!
http://lastbastiongames.com/middleware.html
To answer your question, You can use Vector3.Distance(Input.mousePosition, unitScreenPos) to get the distance from the unit’s pivot point to the mouse cursor in pixels. This is exactly what I do in my RTS engine, too.