Basic RTS kit/project (WIP, Updated June 26th, Buildings!)

So, I’m in the UK right now, and will be for the next month. I would work on my fighting game, but I left the files at home (all of my unity files) and to fill my time, I’ve decided Meh, why not make an RTS kind of system. Just a basic kit that has things like camera movement, unit/building selection, and a basic move command.

So, I’ve been working a little and got camera movement with smooth height control, as well, the camera moves it’s height based on tag information, so it won’t move up or down for being over units or buildings, but will for terrain and water. (the terrain/water could be replaced with anything that has a collider)

Here’s a link to the demo so far. (**Note, it will be updated as I do stuff, so don’t expect it to be the same thing from day to day)

Anyone know where I can get some basic RTS kind of buildings and units for testing purposes? Maybe something that can be distributed with it as examples?

Oh, and this is as a learning project, so I don’t mean to intrude on anyone if they are already making something like this, or if something’s already out.

UPDATE June 16th, 11pm GMT:
Added mouse move support on the edges. Updated the file already, use the link above.

UPDATE June 19th, 1:30pm GMT:
Multiple unit selection now.
I don’t have single unit selection up and running yet. (I think that will be easier)
Anyway, check it out if you want. I’ll either do unit movement or single unit selection next… though, I don’t know how I’ll do pathing at all… (I have no experience with AI besides simple follow player stuff.)

UPDATE June 22nd, 9:30PM:
I’ve now got multi-select, deselect, single select, and additive select.

I only have the four units at the moment, but I will be working on movement, as well as building production next. (:slight_smile: to make more squares)

Oh, also, as you can see in the top left corner, there is a timer… Good for game time lengths, but better for building time periods. :stuck_out_tongue:

UPDATE June 26th, 1:00AM:
Creating units from buildings, and building units finished now. Kind of buggy though on building units, because as soon as you click it de-selects the building.

wow!
like the camera!
could you possibly send me the script, i am trying to figure out an rts, and this camera would be great
triscopeentertainment@hotmail.com
really great!

I haven’t even done mouse movement with it yet, but sure, I’ll probably just post it here though, so anyone that wants it can see it.

I’m not sure how efficient the code is though, being my first time using ray casting (how I decided to tell how high the ground is) so I’m sure I’m doing something incorrectly. (I am fairly new to unity, and haven’t really done anything tricky before.)

EDIT:
I have to get off work first before I can post it. Another 7 hours.

Ok, here is what I have so far for the camera script. Just attach it to your main camera, and add a tag of “CameraStop” to any object with a collider to get it to work right.

var hit:RaycastHit[];
var forward;
var moveSpeed:float = 5;
var mouseMoveSpeed:float = 5; 
var mouseMoveBorder:float = 15;
var distance:float = 10;
var heightGoal:float;

function Awake()
{
	forward = transform.forward;
}

function Update () 
{
    var dTime = Time.deltaTime;
	hits = Physics.RaycastAll(transform.position,forward,Mathf.Infinity,kDefaultRaycastLayers);
	var hitGoals = new Array();
	for (var i=0;i<hits.length;i++)
    {
        var hit : RaycastHit = hits[i];
        if (hit.transform.tag == "CameraStop")
        {
			hitCameraStop = true;
			hitGoals.Add(hit.point.y + distance);
        }
    }
	if(hitGoals.length>0)
	{
		heightGoal = 0;
	}
	for(i=0; i<hitGoals.length;i++)
	{
		heightGoal = Mathf.Max(heightGoal,hitGoals[i]);
	}
	
	if(Input.GetKey("w"))
	{
		transform.position.z += moveSpeed * dTime;
	}
	if(Input.GetKey("s"))
	{
		transform.position.z -= moveSpeed * dTime;
	}
	if(Input.GetKey("a"))
	{
		transform.position.x -= moveSpeed * dTime;
	}
	if(Input.GetKey("d"))
	{
		transform.position.x += moveSpeed * dTime;
	}
	
	var mouse:Vector2 = Input.mousePosition;
	if(mouse.x < mouseMoveBorder)
	{
		transform.position.x -= mouseMoveSpeed * dTime;
	}
	if(mouse.x > Screen.width - mouseMoveBorder)
	{
		transform.position.x += mouseMoveSpeed * dTime;
	}
	if(mouse.y < mouseMoveBorder)
	{
		transform.position.z -= mouseMoveSpeed * dTime;
	}
	if(mouse.y > Screen.height - mouseMoveBorder)
	{
		transform.position.z += mouseMoveSpeed * dTime;
	}
	
	
	transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x,heightGoal, transform.position.z),dTime*2);
}

EDIT:
Added the mouse movement section in. It was much faster to program then I expected. On to basic unit selection.

I’ve got an RTS camera set up. It includes mouse movement based on the mouse position, but no arrow keys. Maybe you would find it useful.

private var myTransform : Transform;
private var myCam : Camera;

var scrollSpeed : float = 100;
var scrollArea : int = 12;	

function Start () {
	myTransform = transform;
	myCam = camera;
}


function Update () {
	var mPosX = Input.mousePosition.x;
	var mPosY = Input.mousePosition.y;

	// Do camera movement by mouse position
	if (mPosX < scrollArea) {
		myTransform.Translate(Vector3.right * - scrollSpeed * Time.deltaTime);
	}
	
	if (mPosX >= Screen.width-scrollArea) {
		myTransform.Translate(Vector3.right * scrollSpeed * Time.deltaTime);
	}
	
	if (mPosY < scrollArea) {
		myTransform.Translate(Vector3.up * - scrollSpeed * Time.deltaTime);
	}
	
	if (mPosY >= Screen.height-scrollArea) {
		myTransform.Translate(Vector3.up * scrollSpeed * Time.deltaTime);
	}		

	myTransform.Translate(Vector3(Input.GetAxis("Horizontal") * scrollSpeed * Time.deltaTime,
	Input.GetAxis("Vertical") * scrollSpeed * Time.deltaTime, 0) );

}

Put this script on an empty gameobject. Then make the camera a child of the gameobject, and you should be set to go.

EDIT: Oops, it seems someone has edited their post and got to this before me. I’ll leave it up anyways.

thanks for the script!
much appreciated!

@Havoc: :slight_smile: Thanks for the code, but as you already stated via edit, I already have it done XD.

@Scope: Glad I could help some.

@All: O-o This process seems to be going faster then I was expecting… Perhaps unit selection will slow me down… Oh well… Bed time for now.

Nighty night all.

@Caliber Mengsk

You’ve got a PM. :slight_smile:

UPDATE:
Multiple unit selection now.
I don’t have single unit selection up and running yet. (I think that will be easier)
Anyway, check it out if you want. I’ll either do unit movement or single unit selection next… though, I don’t know how I’ll do pathing at all… (I have no experience with AI besides simple follow player stuff.)

UPDATE:
I’ve now got multi-select, deselect, single select, and additive select.

I only have the four units at the moment, but I will be working on movement, as well as building production next. (:slight_smile: to make more squares)

EDIT:
Oh, also, as you can see in the top left corner, there is a timer… Good for game time lengths, but better for building time periods. :stuck_out_tongue:

UPDATE:
Creating units from buildings, and building units finished now. Kind of buggy though on building units, because as soon as you click it de-selects the building.

Anyone know how to check to see if the mouse is over a gui element? (whether or not it’s from the same script)
You may have to click the button a few times to get it to work because of this little bug.

Also, I can’t seem to figure out how to get rid of the right click menu, except by going into full screen.

EDIT:
Just a little side note, right clicking on the build unit buttons acts as if you left clicked on it, so it won’t deselect the building if you right click.

To stop the right click menu from popping up in a web build,
all you have to do is change a var in the HTML file.

This link should help you out.
http://unity3d.com/support/documentation/Manual/WebPlayerBehaviorTags.html

Yeah… I searched and found that before posting, but it’s not working.
Maybe it’s cause I use chrome. I guess it’s a bug in the web player at the moment. Either way, it is solved somewhat by going into fullscreen.

Thanks for the help though.

How!!
could u share some of the scripts plz it will help me so much