Creating an RTS in Unity: Part II

Update: This can now also be found at stormtek.geek.nz/rts_tutorial/part2.php, where the entire tutorial is now being hosted.

Last time we installed Unity and got a basic scene up and running. Now it is time to begin to craft our game. Today we will look at creating a player to interact with and using that player to move the camera around. This will also involve our first brush with coding using C# scripting.

Players

We are creating a strategy game that centres around players building up bases and armies in the hopes of map-wide domination. This means that almost everything we do will be through players, whether human or computer controlled. For the purposes of this tutorial we will assume that there will be one human player and one or more computer players. All user input, as well as display back to the user, will be handled by the human player.

Therefore, before we can do anything useful, we must create a player that we can begin to interact with. Create an empty object that will serve as a wrapper for everything related to our player and rename it Player. (GameObject -> Create Empty)

Since we wish to interact with a player, we are going to need some scripts to define what the player can and cannot do. We will use two scripts to do this – one for the player, and one for handling user input for the player. Firstly, to keep things organized as the project grows, lets create a folder inside our assets directory and call it Player. Anything to do with a player will be put inside this folder. Now create two C# scripts inside this folder – call them Player and UserInput.

Drag both of these newly created scripts onto the empty object we created just before to add them to our player object. This will allow us to begin defining behaviour etc. for our player. Well done, you now have the framework for interacting with a player in your game.

Player Details

For now, we don’t need to define much for our new player. In fact, all we want is a way to identify different players, and to determine whether a player is human or not. To do this, we simply need to add two variables to our class.

Open Player.cs in your editor of choice. The only thing that really matters here is the ability to edit the file. Add the following two variables to the top of the class.

public string username;
public bool human;

Once done your Player.cs should like the following:

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

	public string username;
	public bool human;

	// Use this for initialization
	void Start () {

	}

	// Update is called once per frame
	void Update () {

	}
}

We will leave Start() and Update() empty for now, but as our player gets becomes complex later on some extra things will be added. Now return to Unity, select Player, you should see in the Inspector that you can enter a name for your player and define whether it is a human player or not. Enter your name as the username and set your player to be human. The Inspector for your player should look similar to the image below.

Initial Player Settings

Initial player settings

Resource Manager

There is one more thing to add before we get started on the camera. In a number of places we are going to encounter the desire to use some constant values – for things like camera pan, height of the camera off the ground, width of buttons etc. We are also going to want to be able to have easy access to some global variables (there is a time and a place for these) and we will want all parts of the game to be able to access them – eg current map name. Therefore, we are going to create a static class to handle all of this. It shall become our resource manager.

Now, to handle things that are not part of Unity, we are going to create our own namespace – RTS. This will allow us to give any class access to our code by simply adding

using RTS;

to the top of the file. We will store all of our namespaced files in a folder called RTS. Create this folder inside your assests directory now. Inside this folder create a new C# script, rename it ResourceManager, and open it for editing.

There are a number of things we need to do straight away to make this script useful to us. First off, we can get rid of the automatically generated Unity methods, since we will never be using them. Also, make sure that the class no longer inherits from MonoBehaviour, since none of that functionality makes sense, and change it to a static class. Finally, we need to add the namespace declaration around the class definition. The resulting file should look like this:

using UnityEngine;
using System.Collections;

namespace RTS {
	public static class ResourceManager {

	}
}

Note that we still want to use UnityEngine, since that gives us access to all of the Unity framework. Remember, also, that any variables or methods we add to ResourceManager will also need to be declared as static. We will add some details further on as we need them, but for now the basic frame is there for us to build on.

Camera Input

Now it is time to do our first interesting thing for this tutorial – handle user input for the camera. Once this has been sorted, we should not need to touch the camera again, so let’s get it right first time. Open UserInput.cs for editing. As a start lets reference our ResourceManager by adding

using RTS;

to the top of the file. Next up we want a reference to the player for which we are actually handling the input for. Add

private Player player;

to the top of your class. This is a private variable, since we only want this class to be able to interact with it. We will initialize this player when the class is created, since it will never be changing. To do so, add the following line to the Start() method:

player = transform.root.GetComponent< Player >();

This code basically tells Unity to go to the root of the object that this script belongs to (in this case the empty object we created and called Player) and then find the Player component that we added (referenced by the Player.cs script we created just before). Now we can interact with our player directly whenever we want to.

Inside the Update() method add

if(player.human) {
	MoveCamera();
	RotateCamera();
}

Note here that we are only going to handle input for a human player. This means that our update method will do nothing for all computer players, which will help reduce the amount of work that we are doing each update – by keeping this low to begin with we can help to keep our framerate down, making sure that our game runs as smoothly as possible.

We now need to define the two methods that we are calling from inside our Update() method. MoveCamera() will handle moving the camera around our world. Since we are working in 3 dimensions, it will also be useful to be able to rotate the camera to look behind us, which is what RotateCamera() will handle. Create these now, leaving them empty, and making sure that they are both private to make sure that only this class can interact with them.

private void MoveCamera() {

}

private void RotateCamera() {

}

Before we fill in either of those methods, lets just add some speed modifiers to our ResourceManager. For now we will set them to a reasonable speed for us. At a later date these could be updated via an options menu to allow players to customize the environment to best suit their tast. The following values should do it …

public static float ScrollSpeed { get { return 25; } }
public static float RotateSpeed { get { return 100; } }

Camera Movement

Now, let’s make the camera move about the map. We will initiate this whenever the mouse moves to the edges of the screen. To make it a little more forgiving, let’s define an area a few pixels in from the edge of the screen where scrolling will happen. We will do this by adding the following

public static int ScrollWidth { get { return 15; } }

to our ResourceManager. This will allow us to get the width of our scrollable area, but not to change it – which is just how we want it.

The first thing to do inside MoveCamera() is to define the current mouse position and a new vector for our movement, like so:

float xpos = Input.mousePosition.x;
float ypos = Input.mousePosition.y;
Vector3 movement = new Vector3(0,0,0);

Now it is time to handle the panning of the camera around the map. We are going to use the y-axis for height, so the x-axis and the z-axis are to be used for direction in our world. In terms of user input we will use the x-axis for the horizontal screen movement and the z-axis for the vertical screen movement. We use this

//horizontal camera movement
if(xpos >= 0 && xpos < ResourceManager.ScrollWidth) {
	movement.x -= ResourceManager.ScrollSpeed;
} else if(xpos <= Screen.width && xpos > Screen.width - ResourceManager.ScrollWidth) {
	movement.x += ResourceManager.ScrollSpeed;
}

//vertical camera movement
if(ypos >= 0 && ypos < ResourceManager.ScrollWidth) {
	movement.z -= ResourceManager.ScrollSpeed;
} else if(ypos <= Screen.height && ypos > Screen.height - ResourceManager.ScrollWidth) {
	movement.z += ResourceManager.ScrollSpeed;
}

to add movement in the appropriate axis when the mouse is inside the region that we defined for movement – in this case the first 15 pixels in from each edge of the screen.

Now, as I mentioned before, our camera is sitting inside a 3-dimensional world. This means that it is more than likely that our camera has been rotated in some interesting fashion. So if we simply add our movement vector to the position of the camera it will appear to do weird things … (feel free to comment this next bit out at some point and see what I mean). What we want is for our camera to move forwards in the direction that it is pointing. Thankfully, Unity has a method attached to the camera that will do this for us.

//make sure movement is in the direction the camera is pointing
//but ignore the vertical tilt of the camera to get sensible scrolling
movement = Camera.mainCamera.transform.TransformDirection(movement);
movement.y = 0;

That does the trick quite nicely. Note that we are changing the vertical movement back to 0 to ensure that the camera pans around nicely but does not have weird up and down movement at the same time. To add vertical movement we use the scroll wheel on the mouse like so

//away from ground movement
movement.y -= ResourceManager.ScrollSpeed * Input.GetAxis("Mouse ScrollWheel");

and now we are ready to add our calculated movement to the camera. In order to do so, we must first calculate the destination of our camera, which is easy enough to do.

//calculate desired camera position based on received input
Vector3 origin = Camera.mainCamera.transform.position;
Vector3 destination = origin;
destination.x += movement.x;
destination.y += movement.y;
destination.z += movement.z;

Now that we know where we want the camera to go, there is one final check we wish to make. To keep things sensible we should make sure that the user cannot move the camera down through the ground, and also that they cannot move it so far up that nothing can be seen clearly anymore. Add the following limits to our ResourceManager

public static float MinCameraHeight { get { return 10; } }
public static float MaxCameraHeight { get { return 40; } }

for the minimum and maximum heights allowed for our camera. This will work fine for our world, since the ground will always be flat. However, if you add terrain at a later date you will need to rethink how this is being calculated. Now we can add the following check to MoveCamera() to keep the camera inside the limits that we have set.

//limit away from ground movement to be between a minimum and maximum distance
if(destination.y > ResourceManager.MaxCameraHeight) {
	destination.y = ResourceManager.MaxCameraHeight;
} else if(destination.y < ResourceManager.MinCameraHeight) {
	destination.y = ResourceManager.MinCameraHeight;
}

At last we are ready to add the desired movement to the camera itself, which we will only do so if the camera has moved. Unity nicely provides us with a static reference to the main camera which we will make use of.

//if a change in position is detected perform the necessary update
if(destination != origin) {
	Camera.mainCamera.transform.position = Vector3.MoveTowards(origin, destination, Time.deltaTime * ResourceManager.ScrollSpeed);
}

If you now go into Unity and hit play, you should be able to move your camera around the map with no trouble at all.

Camera Rotation

The final thing to cover in this post is rotating the camera. We will do this when the user holds down the right mouse button. However, we will also be using the right mouse click to deselect items later on, so we need to throw a modifier key into the mix to prevent confusion. We will allow the player to rotate the camera, but only if they are pressing the ALT key while holding down the right mouse button. This will prevent any accidental movement of the camera. Add the following code to the RotateCamera() method.

Vector3 origin = Camera.mainCamera.transform.eulerAngles;
Vector3 destination = origin;

//detect rotation amount if ALT is being held and the Right mouse button is down
if((Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt)) && Input.GetMouseButton(1)) {
	destination.x -= Input.GetAxis("Mouse Y") * ResourceManager.RotateAmount;
	destination.y += Input.GetAxis("Mouse X") * ResourceManager.RotateAmount;
}

//if a change in position is detected perform the necessary update
if(destination != origin) {
	Camera.mainCamera.transform.eulerAngles = Vector3.MoveTowards(origin, destination, Time.deltaTime * ResourceManager.RotateSpeed);
}

Take note that we are allowing rotation around the camera’s vertical axis as well as tilting up and down to change the angle with which we are viewing the ground. It turns out that it is a lot easier to rotate the camera than it is to move it around sensibly.

Congratulations! You now have a working real-time strategy style camera that can comfortably move around your world, handling 3 dimensions comfortably. Feel free to take it for a spin and get a feel for how we will be navigating the world we are building up.

For completion this week, here is the completed code for our UserInput and for our ResourceManager. Remember, all of the code can be found on my github account. Changing to the commit for the part you are working through will give the state of the code at the end of that part (in this case it is part 2). From now on I will generally not post full code here – just the lines we are adding (plus maybe a little extra for context to make sure you stay on track).

UserInput
using UnityEngine;
using System.Collections;
using RTS;

public class UserInput : MonoBehaviour {

	private Player player;

	// Use this for initialization
	void Start () {
		player = transform.root.GetComponent< Player >();
	}

	// Update is called once per frame
	void Update () {
		if(player.human) {
			MoveCamera();
			RotateCamera();
		}
	}

	private void MoveCamera() {
		float xpos = Input.mousePosition.x;
		float ypos = Input.mousePosition.y;
		Vector3 movement = new Vector3(0,0,0);

		//horizontal camera movement
		if(xpos >= 0 && xpos < ResourceManager.ScrollWidth) {
			movement.x -= ResourceManager.ScrollSpeed;
		} else if(xpos <= Screen.width && xpos > Screen.width - ResourceManager.ScrollWidth) {
			movement.x += ResourceManager.ScrollSpeed;
		}

		//vertical camera movement
		if(ypos >= 0 && ypos < ResourceManager.ScrollWidth) {
			movement.z -= ResourceManager.ScrollSpeed;
		} else if(ypos <= Screen.height && ypos > Screen.height - ResourceManager.ScrollWidth) {
			movement.z += ResourceManager.ScrollSpeed;
		}

		//make sure movement is in the direction the camera is pointing
		//but ignore the vertical tilt of the camera to get sensible scrolling
		movement = Camera.mainCamera.transform.TransformDirection(movement);
		movement.y = 0;

		//away from ground movement
		movement.y -= ResourceManager.ScrollSpeed * Input.GetAxis("Mouse ScrollWheel");

		//calculate desired camera position based on received input
		Vector3 origin = Camera.mainCamera.transform.position;
		Vector3 destination = origin;
		destination.x += movement.x;
		destination.y += movement.y;
		destination.z += movement.z;

		//limit away from ground movement to be between a minimum and maximum distance
		if(destination.y > ResourceManager.MaxCameraHeight) {
			destination.y = ResourceManager.MaxCameraHeight;
		} else if(destination.y < ResourceManager.MinCameraHeight) {
			destination.y = ResourceManager.MinCameraHeight;
		}

		//if a change in position is detected perform the necessary update
		if(destination != origin) {
			Camera.mainCamera.transform.position = Vector3.MoveTowards(origin, destination, Time.deltaTime * ResourceManager.ScrollSpeed);
		}
	}

	private void RotateCamera() {
		Vector3 origin = Camera.mainCamera.transform.eulerAngles;
		Vector3 destination = origin;

		//detect rotation amount if ALT is being held and the Right mouse button is down
		if((Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt)) && Input.GetMouseButton(1)) {
			destination.x -= Input.GetAxis("Mouse Y") * ResourceManager.RotateAmount;
			destination.y += Input.GetAxis("Mouse X") * ResourceManager.RotateAmount;
		}

		//if a change in position is detected perform the necessary update
		if(destination != origin) {
			Camera.mainCamera.transform.eulerAngles = Vector3.MoveTowards(origin, destination, Time.deltaTime * ResourceManager.RotateSpeed);
		}
	}
}
ResourceManager
using UnityEngine;
using System.Collections;

namespace RTS {
	public static class ResourceManager {
		public static int ScrollWidth { get { return 15; } }
		public static float ScrollSpeed { get { return 25; } }
		public static float RotateAmount { get { return 10; } }
		public static float RotateSpeed { get { return 100; } }
		public static float MinCameraHeight { get { return 10; } }
		public static float MaxCameraHeight { get { return 40; } }
	}
}
Advertisements

49 thoughts on “Creating an RTS in Unity: Part II

      • Paletizma says:

        I see you are good at programing, have you ever thought of making your own rts game? I’m a modeller myself, and i’m searching for a good programer, to develop a new rts game, something like Rise of Nations 😉 If you are interested, I will be very glad to share my contact, if not, I can always provide you with models and textures 🙂

  1. unlucky says:

    I have this weird camera problem I can’t seem to figure out.
    once I start/run the game the camera just slowly moves down.
    this is where I thing the problem is:

    //calculate desired camera position based on received input
    Vector3 origin = Camera.mainCamera.transform.position;
    Vector3 destination = origin;
    destination.x += movement.x;
    destination.y += movement.y;
    destination.z += movement.z;

    it just wont work, tried everything I could find but still nothing.
    ERROR:

    Assets/custom Assets/Player/UserInput.cs(50,56): error CS0571: `UnityEngine.Component.transform.get’: cannot explicitly call operator or accessor

    I’m not a c# programmer so I don’t know what that means, Please Help.

    • Neinth says:

      Your error is on line 50 as it says here “Assets/custom Assets/Player/UserInput.cs(50,56): error CS0571: ”

      Copy and post line 50,so people can see what is throwing the error

  2. If your code matches my code *exactly* then line 50 should be

    Vector3 origin = Camera.mainCamera.transform.position;

    and it does look like the compiler is complaining about getting something from a UnityObject.transform. What is the exact code that you have for that line?

  3. Hello, I was trying to compile but some how I’m having an issue with the UserInput.cs. Everytime I try to compile it gives me an error saying that: using RTS; cannot find the namespace or the type. the: using RTS; is on Line 1 just like you’ve wrote so I’m not sure why it’s always showing up as an error.

    Thank you

  4. Mr.Ferret says:

    Im not sure why but it doesnt seem to move at all for me..cant do anything just getting

    NullReferenceException
    UnityEngine.Component.get_transform () (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/ExportGenerated/Editor/UnityEngineComponent.cs:21)
    UserInput.MoveCamera () (at Assets/Root/Player/UserInput.cs:43)
    UserInput.Update () (at Assets/Root/Player/UserInput.cs:17)

    when i run…any ideas?

    • What is UserInput.cs line 43 for you? The error message is telling you that is the line that is breaking. In fact, it is probably easiest if you could post the entirety of UserInput.cs (as an attachment if that is possible) so that I can have some more information to work with.

      • Hi I was just wondering I’ve got the same problem as Mr.Ferret I’ve put the code below I don’t seem to know what the problem is. Thanks in advance.

        //make sure movement is in the direction the camera is pointing
        //but ignore the vertical tilt of the camera to get sensible scrolling
        movement = Camera.mainCamera.transform.TransformDirection(movement);
        movement.y = 0;

        • I wonder if this is related to a change in the version of Unity. If you are using Unity 4.2 then they changed the api that relates to the main camera. They now use

          Camera.main

          rather than

          Camera.mainCamera

          to reference the main camera. If they do not have decent backwards compatibility then it is entirely possible that this would give a null reference exception, which is what MrFerret was mentioning.

    • Hopefully this can help some folks. I had the same problem at first. I double checked the code, even used Devart Code Compare. Still wasn’t working. Then I noticed that the Player object I created at the beginning of the tutorial seemed to be gone. I wasn’t sure how that happened, but I created it again, dragged the Player.cs and UserInput.cs scripts onto it, just like the tutorial had me do. Finally, I checked the Player settings box acknowledging that I am, in fact, human (the one marked “human” :)). That was it. Now I was off and running (flying) as expected. Hope that helps someone. Elgar, thanks so much for taking the time to post all of this. It is so incredibly helpful and you are a god among men for helping the rest of us out so much. Best of Luck!

  5. John says:

    I can’t find the method attached to the camera you talked about in “Thankfully, Unity has a method attached to the camera that will do this for us”.

  6. John says:

    Just a note that when I compile the project, it says “Assets/Player/UserInput.cs(70,41): warning CS0618: `UnityEngine.Camera.mainCamera’ is obsolete: `use Camera.main instead.'”.

    • Have you upgraded your version of Unity? Cos I know that Unity 4.2 has some slightly different ways of referencing some things (having worked on a separate project in Unity 4.2 I noticed this).

      • Adam says:

        Hah – just stumbled upon this myself. In script UserInput.cs, doing a search and replace on “Camera.mainCamera” and changing it to “Camera.main” fixed it. Must be some changes in Unity 4.2 (I’m using 4.2.3 here).

  7. Dan says:

    Hi,

    Thank you for this great tutorial – very informative without spoon-feeding.
    I am having a strange problem though, where everything compiles correctly and Unity enters Play mode, but the camera doesn’t budge an inch. (no panning or rotation)
    I’m using Unity 4.3.2f1 and I’ve changed the Camera.mainCamera reference to simply Camera.main and since then it’s compiled fine. But, yeah, no movement. =(

    Any suggestions on what I’m doing wrong/what I can do to fix this, would be great.

  8. Dan says:

    Just solved the problem. It was a PEBKAC error, forgot to add the scripts to Main Camera.
    Still a great tutorial though. 😉

  9. Jacob says:

    I don’t know if this is just on my end, or if no one else has noticed it, but I’m getting a weird bug so far with this code. Everything works just like it should, but if I look straight up or straight down with the ALT + Right Click function we just added, the camera gets stuck at that angle. Is there a way to fix this, or did I do something wrong? I’m trying to fix it myself but I don’t understand the angle math quite well enough I guess.

    • That sounds entirely like it could be an unexpected bug. I never stress tested things that hard. So an edge-case like that doing weird things would not really surprise me.

      • Jacob says:

        Found a work-around for anyone needing it, Nothing too complex, and it may be kind of confusing. Before moving the camera towards the destination with the Vector3.moveTowards command, add this.

        //limit from straight up and down
        if (destination.x > 75 && destination.x < 90) {
        destination.x = 75;
        } else if (destination.x 90) {
        destination.x = 320;
        }

        • blake says:

          I did something similar. I used a variation on the height bounding from the movement method.

          ResourceManager:

          public static float MaxVerticalCameraAngle { get { return 40; } } // Limit Camera to 40 x max rotation (looking down at 45 degrees)
          public static float MinVerticalCameraAngle { get { return 0; } } // Limit Camera to 0 x min rotation (horizon)

          UserInput:RotateCamera()

          // Bound the vertical rotation
          if ( destination.x > ResourceManager.MaxVerticalCameraAngle) {
          destination.x = ResourceManager.MaxVerticalCameraAngle;
          } else if ( destination.x < ResourceManager.MinVerticalCameraAngle) {
          destination.x = ResourceManager.MinVerticalCameraAngle;
          }

  10. I’m having an error in my code in that it moves but only in two directions – and these are activated as move towards top left screen when either top or left side is activated with mouse and vice versa for bottom right. Also my height scroll does not work with the scroll wheel-here is the code:
    private void MoveCamera(){
    //gets the position of the y and x axis of the mouse position
    float xpos = Input.mousePosition.x;
    float ypos = Input.mousePosition.y;
    //sets up a 3 dimensional vector
    Vector3 movement = new Vector3 (0, 0, 0);

    //horizontal camera movement
    //if the x position is greater or equal to 0 and smaller than the scroll width(ie it must be near the edge)
    if (xpos >= 0 && xpos < ResourceManager.ScrollWidth) {
    movement.x -= ResourceManager.ScrollSpeed;
    //same as above but this checks whether it is going to opposite direction
    }else if(xpos Screen.width – ResourceManager.ScrollWidth){
    movement.x += ResourceManager.ScrollSpeed;
    }//end if

    //vertical camera movement
    //same as all above
    if(ypos >= 0 && ypos < ResourceManager.ScrollWidth){
    movement.z -= ResourceManager.ScrollSpeed;
    }else if(ypos Screen.height – ResourceManager.ScrollWidth){
    movement.z += ResourceManager.ScrollSpeed;
    }

    //this makes sure that movement is in the direction the camera is facing
    movement = Camera.main.transform.TransformDirection (movement);
    //ignores the vertical axis so it does not have weird up and down movements – easier to do with scroll wheel on mouse
    movement.y = 0;

    //This is away from the ground movement
    movement.y -= ResourceManager.ScrollSpeed * Input.GetAxis (“Mouse ScrollWheel”);

    //calculate desired camera position based on received input
    Vector3 origin = Camera.main.transform.position;
    Vector3 destination = origin;
    destination.x += movement.x;
    destination.y += movement.y;
    movement.z += movement.z;

    if (destination.y > ResourceManager.MaxCameraHeight) {
    destination.y = ResourceManager.MaxCameraHeight;
    } else if (destination.y < ResourceManager.MinCameraHeight) {
    destination.y = ResourceManager.MinCameraHeight;
    }

    //if a change in position is detected perform the necessary update
    if (destination != origin) {
    Camera.main.transform.position = Vector3.MoveTowards (origin, destination, Time.deltaTime * ResourceManager.ScrollSpeed);
    }

    }//end MoveCamera()

    • First off, it looks like the else checks for both horizontal and vertical movement are not quite right. WordPress may have removed a character, but the check still does not look long enough.

      If you are getting no change in camera height at all there are a couple of things I would check. First, is ResourceManager.ScrollSpeed returrning a non-zero value (I presume it is since we use that to change the entire camera position as well). Second, is the Input Axis “Mouse ScrollWheel” actually present – it is possible that the name of this changed in Unity for some reason (though no one else has mentioned having a problem). To check this, go to Edit -> Project Settings -> Input, expand the Axes object that appears in the Inspector, and look for a value titled “Mouse ScrollWheel”. This should have Type = Mouse Movement and Axis = 3rd Axis.

      Hope that helps you track some things down.

      • I have managed to get the camera movement x + z working the problem was “movement.z += movement.z;”
        Very stupid – however the scroll wheel will not work – it says exactly as you said in your reply “This should have Type = Mouse Movement and Axis = 3rd Axis.”

        • I did a debug.log of movement.y and i got – 54 around – is this a problem
          I then did a debug.log of “Input.GetAxis (“Mouse ScrollWheel”)” and got between -0.01 and -2 – is this a problem – here is the code again: movement.y -= ResourceManager.ScrollSpeed * Input.GetAxis (“Mouse ScrollWheel”);

          public static float ScrollSpeed { get { return 25; } }

        • PS – i didn’t just copy and paste show that is why the above error happened – however i prefer not to copy and paste as it is easier to understand the code writing it out

          • I agree that straight copy / paste is not the best idea. When following something like this I too find it much better to write all of the code out. The act of writing it helps build understanding of what is going on, even if it does take a bit longer. Of course, as you have found, the chances of errors because of typing mistakes are a lot higher.

  11. Brad says:

    I encountered an error that read “The class defined in script file named Userinput does not match the file name.” I actually have no idea what this means, and any help would be appreaciated 🙂

    PS My userinput code matches yours exactly.

    • C# requires that the name of the file and the name of the class defined in that file match exactly – and this is a case-sensitive match too. If you have created the file Userinput.cs with the class UserInput defined inside it then it will complain.

  12. Valt says:

    I got an error from ResourceManager.cs “CS0101: The namespace ´RTS´ already contains a definition for ´ResourceManager´” what could be the problem?

  13. Pioneer says:

    Hi, thanks for great tutorial. New to all this but eager to learn. All scripts in this tut i put in player, didnt put any scripts in my MainCamera. Should i do that?? (Sorry if missing anything) Anyway the scroll zoom work as to the move of camera in screen edges. Cant rotate anything though, should i be able? Even so i dont want any rotation, just the overview.

    In my game i wont use any “Worker” or actual “Player” as the player will be using only the mouse for actions such as creating units, upgrade buildings. Guess “Player” in this case refer to my solution also 🙂

    Thanks again for a great tutorial!!

    • At the end of the day it does not really matter where you put your scripts (although some of them are expecting to be somewhere relative to another script so that they can find each other and communicate). Having said that, I find it a lot easier to keep track of things if scripts are added to logical sounding things. So in this case I am using Player since the user is a player interacting with things.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s