I am currently developing a RTS game, and I need to be able to do a few things but I can’t seem to get them working.
What I need help with:
- Camera Zoom - I’ve tried numerous codes given to me by others but none seem to actually work, my attempts have also failed.
- Selecting a group of units with mouse drag and highlighting them.
- Moving selected units to where the mouse has clicked with a raycast.
The language I use is C# but I’m sure I can translate from the others languages rather easily.
Thanks in advance for any help that can be given.
Hello, I can’t help you in detail but some tipps:
- When you zoom in, you just need to Lerp / Move the camera closer to the direction it’s looking at. I believe it’s the Z-direction of the camera in localSpace. The 3rd party library iTween can give you all kinds of smooth translate effects.
- I am not 100% sure with this, but I recall from some old threads in this forum that in order to select units, you need to scale an invisible cube on the specific place in the world. This cube needs a collider attached (as a Trigger) and while it scales up (by pressing and holding the mouse button) and hits player units in the world, it registers them on a list.
- There is a function Camera.ScreenPointToRay which allows you to hit the world at the place where you clicked with your mouse. Set this as a target point for player units.
I am working on an RTS as well. I wrote some very simple code for a Camera Zoom type functionality that uses the mouse scrollwheel. But it’s very crude, and doesn’t “interpolate” well, it just moves the camera in along the y-axis when you scroll the mousewheel. Here is the code:
//Beginning of mouse scroll wheel zoom functionality
public var camMaxZoom : float = 12;
public var camMinZoom : float = 1;
function Update() {
if(Input.GetAxis("Mouse ScrollWheel") > 0 Camera.main.transform.position.y > camMinZoom) {
Camera.main.transform.position.y -= .7;
}
if(Input.GetAxis("Mouse ScrollWheel") < 0 Camera.main.transform.position.y < camMaxZoom) {
Camera.main.transform.position.y += .7;
}
}
Attach this script to your main camera. And set up the camMinZoom and camMaxZoom variables to be whatever you want.
As for selecting units via a selection box, that is more complicated. What I do, is I detect when the player is holding down the left-mouse button, and if the mouse moves more than say a distance of 10 pixels on-screen, then you start drawing a GUI box (using Unity’s GUI system). Then when the player lets go of the mouse button (after dragging a box), I use a semi-complicated math formula to detect whether or not the unit’s screen position was inside the x,y values of the selection box.
I’m sorry I don’t have time to give a great example or tutorial right now, but there are many cool resources for learning some rts-type game things. I would recommend looking at the RTS Binary tutorials on YouTube (by Metalstache). Just search for RTS Binary on YouTube.
Good luck. RTS’s are pretty complicated, but very rewarding as well. Just keep searching for examples and asking questions on the forums.