- Home /
RTS Game: Unit selection through Raycast
I am making an RTS game and, of course, need to be able to select units. I spent 3/4 of today trying to make it work by myself, the other 1/4 of the day I spent following tutorials, but none of them worked. I would just like a hint or a push in the right direction, for I want to accomplish at least a bit by myself.
P.S.: I am not new to programing or Unity3D, I am no noob.
Perhaps you could give us a little more information on what you've tried so far, and what you mean by "none of them worked"?
Is there a specific concept you're getting stuck on? Code that you implemented that isn't behaving like you expect it to?
I have RTS-style unit selection working in my game using raycasts. So I could help you but as ^ said, you need to post sections of script and/or screenshots showing what you were trying to do and how it is failing.
Answer by Kiloblargh · Jan 05, 2014 at 10:08 PM
In the game I'm currently working on, I have a MasterControl script that keeps track of these things (among many others...)
selectedMover : Unit
remoteSelectedMover : Unit
moveTarget : Transform
remoteMoveTarget : Transform
Every unit has a script on it- and it's the same one script, it's just configurable to be any controllable unit in the game.
The Unit script's TakeClick() function is called from an input script on the main camera that does the ray cast, using a LayerMask set to selectable units only:
hit.collider.gameObject.GetComponent.<Unit>().TakeClick();
This function checks with the MasterControl script to see if the unit belongs to the player or is an enemy, and if the selectedMover variable is null, the clicked unit, or another unit; and decides based on that what needs to happen next.
The Unit script has a Select() function and a Deselect() function that shows/hides selection graphics FX and plays a short animation and sets its "isSelected" variable to true or false.
I still need to extend what I have to allow group selecting and giving move-attack orders to multiple units at once. Selecting just one at a time is not acceptable for a final game (well, maybe, if you're doing something more XCOM than StarCraft...) but you should probably still start out with selecting one unit at a time with the thought of how to make it work with groups- the devil is in the details. For my game I can replace all my single variables with Lists and won't have to change much else.
Answer by Qo2770 · Feb 01, 2014 at 11:20 AM
Thanks, Sorry that I was not answering but my computer broke down...
Thanks for the help though! I think that is what I was looking for!!
Qo2770