• Home
  • Tutorials
  • Using Keyboard Input to Move Objects or Characters in Unity 3
 
40
0
0

Using Keyboard Input to Move Objects or Characters in Unity 3

RyanviaUnity Tutorials
January 11, 2011

Almost any game requires some type of movement – whether it be a first person shooter, a RPG, a puzzle or even a strategy game – either elements of the game (like a door), or the character in the game requires some type movement.  There are 3 basics types of movement within Unity (and most games for that matter) – physics based movement, input based movement or scripted movement (Artificial Intelligence, path finding, animations etc.). Unity provides us with a variety of functions and features that we can use to implement anything from simple input based movement to complex physics based movement, such as driving a car.

Moving a Character  or Object Using Input Based Movement

So what is input based movement? Well, it’s exactly what you you would expect it to be. When the player provides “input” – such as a button on a controller, or hitting a key on the keyboard, or clicking the mouse buttons – your game character or objects within the game react to the input. Without user input there would be no way to interact with the game and it wouldn’t really be a game at all. That wouldn’t be much fun now would it? So with that said, working with player input is a very important aspect of any game. In some cases it can even make or break a game.

Using Keyboard Input in Unity

We’ll start off by looking at how we can use keyboard input to move things in Unity. Here’s a simple example. This script should be attached to the object you want to control with the keyboard.

var speed : float = 10.0;
 
function Update() {
 
	//check if the user is pressing left or right on the keyboard
	//by default Unity assigns the "a" and "d" key as well as the left and right arrow keys to the horizontal axis.
	horMovement = Input.GetAxis("Horizontal");
	if (horMovement) {
		transform.Translate(transform.right * horMovement);
	}
}

Let’s break this down further..

First we assign a value for the speed we want to move at. This variable will be available in the inspector so you can change it on the fly.

Next, in the Update() function, we determine if the user is pressing right or left on the keyboarding using either a, w or the left and right arrow keys. The function “Input.GetAxis(“Horizontal”)” returns a value of either -1, or 1 depending on which key the user is pressing. If the user is pressing left on the keyboard the value will be -1, if they are pressing right the value will be 1. If they aren’t pressing anything the value will be 0.

Next, we use an If statement to see if the user is pressing a key. If the user is pressing a key we use the Translate function to move the object. Let’s look at the transform.Translate line.

transform.Translate(transform.right * horMovement * Time.deltaTime * speed);

In this line of code we take the transform.right and multiply it by the Input.GetAxis value – remember this will be either -1, or 1.  Multiplying by the Input.GetAxis value will make sure we are moving in the right direction (negative or positive). Next we multiply it by Time.deltatime * speed to add some speed smoothing to the movement. This prevents jerky movements and gives us more control over the speed from within the inspector.

Ok, so we have horizontal movement, let’s see if we can add some vertical “up and down” movement to our object now as well.  You should try to do accomplish this on your own then check back here for the solution – I’ll give you a hint – the code requires only 2 changes.

Here’s our finished code now with horizontal and vertical movement.

var speed : float = 10.0;
 
function Update() {
 
	//is the user pressing left or right (or "a & "d") on the keyboard?
	horMovement = Input.GetAxis("Horizontal");
	//is the user pressing up or down (or "w" & "s") on the keyboard?
	vertMovement = Input.GetAxis("Vertical");
 
	if (horMovement) {
		transform.Translate(transform.right * horMovement * Time.deltaTime * speed);
	}
 
	if (vertMovement) {
		transform.Translate(transform.up * vertMovement * Time.deltaTime * speed);
	}
}

Now if you run this game you will be able to move your object up and down and left to right on the X and Y axis. You may also notice that you can move at an angle by pressing 2 keys at once. If you only want your object to move in one direction at a time, you can modify the code to make sure only one key is being pressed at a time.

	if (horMovement && !vertMovement) {
		transform.Translate(transform.right * horMovement * Time.deltaTime * speed);
	} 
 
	if (vertMovement && !horMovement) {
		transform.Translate(transform.up * vertMovement * Time.deltaTime * speed);
	}

See how that works? Now the vertical movement statement will only be evaluated if the user isn’t pressing left or right and vice versa.  Keep in mind this usually isn’t a desirable effect.

Moving in 3d Space Instead of 2d

Up until now the examples have only moved our object on the X and Y axis. In other words our object is still only moving in 2d space. This is because we’re using transform.up (the Y axis) and transform.right (the X axis). To move in3d space we will use transform.forward (the Z axis).

Let’s modify our original code.

var speed : float = 10.0;
 
function Update() {
 
	//is the user pressing left or right (or "a & "d") on the keyboard?
	horMovement = Input.GetAxis("Horizontal");
	//is the user pressing up or down (or "w" & "s") on the keyboard?
	forwardMovement = Input.GetAxis("Vertical");
 
	if (horMovement) {
		transform.Translate(transform.right * horMovement * Time.deltaTime * speed);
	} 
 
	if (forwardMovement) {
		transform.Translate(transform.forward * forwardMovement * Time.deltaTime * speed);
	}
}

Great, now when we press up or down our object moves on the Z plane instead of the Y plane. This essentially moves our object further away and closer in 3d space. Pressing left or right still moves our object on the X plane. This is generally how most 3d games are setup. Your objects Y plane will only be used for actions like jumping and your forward movement will occur on the objects local Z plane while left and right movement occurs on the Local x plane.

While these examples are a good introduction to moving objects, Unity also provides us with many other ways to move our characters and objects and we generally don’t have to modify the transform of the object directly. One component that comes in very handy – especially for first person shooters – is Unity’s Character Controller component, we’ll look at that next.

Discussion

-