Hi all,
I’ve been working on an RTS-style marquee selection mechanic for a 3D game that allows you to select the gameobjects within the selection. I’ve got the marquee itself working, but I’m trying to figure out how I could get all the objects within that marquee selection, and add them to an array?
To draw the marquee, I’m doing something similiar to what’s described here. I also found this, but it uses the old GUI system, and I’m using the new one. I also tried to come up with a solution by myself by checking if an object’s x- and y-coordinates fit within the selection, but that only worked when the marquee was drawn from upper left to lower right. It was also quite CPU-heavy when the object count was at 50 or more.
I also thought about doing some analytic geometry by checking if the object is within the drawn rectangle. An example of that is here. The problem with this was that it was just a bit too heavy on the CPU when the object counts started to rise.
If someone could point me into the right direction, that’d be great
This is just a brain dump, so I am sorry if its a bad idea or stupid!
I guess this 2D right? could you not have a collider set as trigger that you scale and position according to the marquee? Then you could use its OnTriggerEnter event to catch the objects that enter it?
The game is actually 3D, I’ve updated the main post to include that info.
I’d rather not use triggers, as scaling them according to the marquee on an uneven surface could be problematic. A fully code-based solution would be preferrable.
OK well, again another brain dump so forgive me - I am on a train at the moment…, in my opinion it is still a 2d problem because the unit height is irrelevant, right?
If I had to develop the same system, I would:
Take the 2D marquee extents and project that as a plane into the world.
Check what units are inside this plane (ignoring the height part - just assume all height is zero) - I thought a single trigger collider, rotated and and scaled to match the projected plane might work, or you could try a AABB
If you are dealing with many units, you could split your terrain into sectors, then see what sectors the marquee crosses and only test units that are in those sectors.
That said, this has really got me interested. When I get access to Unity later I will try and see if I can come up with a solution.
Sorry for the hideous spelling, punctuation and grammar!
Hmm… I might take a look into AABB when I get back on the computer, but I don’t think the plane approach would work, because the marquee is a 2D rectangle, and the plane would be a 3d object. If I had a plane laying flat on the ground, some objects that would otherwise be selected, couldn’t be selected, as the plane would go through the map on uneven areas. If I made it so that the plane would be positioned perpendicularly to the ground on even and uneven areas, again, some objects may not get selected because the plane would go through the ground and not collide with the gameobjects.
I’ll take a look into AABB and probably some analytic geometry again, as I think one of those two could be the solution.
Alright - I got it to work Basically, I have an UI Image being rendered on screen representing the marquee area - but I also have a Rect that is scaled with the marquee, and then I’m using Rect.Contains to check if any of the gameobjects are within the rect. If they are, they are selected.
This video was a great help:
Here’s a link to a short screen capture of the selection in action - I had to start the recording from my desktop, as Unity crashed if it was open when I started recording