Unity Basic: Object Movement


The most common operation when you play a game is moving the player whether it in First Person Shooter game or Third Person shooter game. To move a player which we call here as an object, we need to read what buttons pushed by the user. For example if we use our keyboard, we will push the left arrow button or “A” button to move an object to the left. Based on this input we will then move our object to the left. When we able to move our object to the left we will need to make our object to face that direction in order to make it more natural. In this article we will learn the movement of objects in Unity.

Before reading this article, it is assumed that you know basic operation in Unity including create new project, create new script, create new object and basic understanding of Javascript.

Reading Input

As we want to read the input from the keyboard, we will need to understand of how to address them in in code first. Unity has many inputs that can be viewed and adjusted using the Input Manager through Edit menu -> Project Settings -> Input

The default settings for Input in Unity are Horizontal and Vertical.  These based on input when we holding the positive button builds to a value of 1 and when holding the Negative Button builds to a value of -1. Input is also the name of a class and it has elements that consist of axes and buttons. In Scripting we can simply write:

Input.GetAxis(“Horizontal”);

This script will read the horizontal left and right arrow button in your keyboard and then we can move our object.

Translate

To move an object we need to use Translate command. You can read more about Translate in Unity Script Reference which part of Transform Class. This class stores the position, rotation and scale properties of an object and also some functions to move and rotate the object.

an example of the usage of Translate is as follows:

Transform.Translate(x,y,z);

Implementation

Now let us try to implement to read input from keyboard and use Translate to move our object. We will read vertical (v) and Horizontal (h) input values. We will also need to define the speed of the movement.

Create a new script in your unity project pane by clicking in your right button and choose create -> Javascript. Write the code below and save it as movement.js.

var power: float =1500;
var moveSpeed: float = 5;
function Update () {

	if (Input.GetAxis("Horizontal") || Input.GetAxis("Vertical"))
	{
		var h :float=Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
		var v:float=Input.GetAxis("Vertical") * Time.deltaTime*moveSpeed;
		transform.Translate(h,0,v);
	}
}

To apply the code in your object, click the object that you want and drag movement.js on to the object. Click Play button and push your right or left arrow to see the movement of your object.

Facing Direction

When we push the direction we want in our keyboard, our object will move to that direction. Now imagine if our object is a character. We will need to make our character to face to the direction that we want. To do this we will need to call Transform.forward. It is the “forward” orientation of the transform which is the positive Z direction in local space. So when we assign the direction, Unity will automatically rotate your object its Z axis is the same as our supplied forward vector.

In the example below, we use Input.Getkeydown which will read the button the pushed by the user. Here we will try to detect if user push one of four buttons “W,A,S,D” and rotate our character to that direction.

if (Input.GetKeyDown(KeyCode.W))
	        transform.forward = new Vector3(0f, 0f, 1f);
	    else if (Input.GetKeyDown(KeyCode.S))
	        transform.forward = new Vector3(0f, 0f, -1f);
	    else if (Input.GetKeyDown(KeyCode.A))
	        transform.forward = new Vector3(1f, 0f, 0f);
	    else if (Input.GetKeyDown(KeyCode.D))
	        transform.forward = new Vector3(-1f, 0f, 0f);

Your comments are welcome

 

 

Incoming search terms:

Related Posts:

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>