Hello everyone, I’m currently working on a simple RTS sample in unity and when working on the methods to select the units in the scene I noticed there is a lack of being able to collect all the objects within a square (Without having to be hacky and placing a square in the scene just to pick off of) is there any other method?
Not that I know of. Also, what do you mean hacky? Creating your own method do something isn’t “hacky”, lol, it’s creating something that isn’t built in, which is most of the game and half the fun :P. If your looking for a quick fix, I don’t think there isn’t one : /. I’ll try and make something like that real quick now, just as a fun little challenge, lol. I’ll let you know if I come up with anything.
The reason why I would call it hacky is that I have to create an object in the scene that is only used for a scan for all the objects within it. I guess I could just script the creation of a square collider.
Ok, so I’ve been working on this since my last post, and I have a nice little draggable selection box going, but it seems the problem depends on your camera. Is it a fixed distance from the ground at all times? Is the terrain flat?
How would we know? That’s entirely dependent on your camera and your terrain.
In the case of my project, its a game in space, so I can use the maximum depth of the ships movement.
Ok. That makes it more difficult. I’ll see what I can whip up.
Meh, proving to be difficult. I got something working but it’s very slow. I’ll PM you if I get around to polishing/making it usable.
I figured out an interesting solution, rather then doing something crazy like checking collision. I just go thru the possible selected objects and convert there position to screen space, then i check to see if the selection rectangle contains the position.
Haha, yes. That was my solution also. As I said, I have a working solution, I just didn’t feel it was good enough to post. I did the same thing, however, if there are anything over 100 or so possible gameobject’s it’s too slow. Also, are tracking who is on screen? I used FindGameObjectsWithTag to grab everything with the “Selectable” tag, and search through those. Like I said though, the FindGame… is much too slow, there is a hiccup when this function is called when you have more than 100 or so units in the game. Try it. The only solution I could think of then was to keep a running array of selectables on screen, this way you could just search that list, rather than using the all-too-slow FindGameObjectsWithTag function. Anyways, glad you’ve solved it and are satisfied :).
P.S. Do you have a solution the problem I’ve pointed out? Or are there just very few GO’s in the scene? Or have you used another method to grab potential objects?
I was going to use the same exact idea as you, and keep a running list of selectable objects haha
If there was a function to get a list of all objects in a cameras view that would work.