Simple moving 2D Object

Welcome to our Unity tutorial series.

In this tutorial, we will show you how to make an object move in 2D space.

We think you can understand how important it is to make an object like player or enemy move in your game, even simple games like Snake or Pacman have object moving.

There are a lot of ways to make an object move with Unity, we will show you how to do it in an easy way. If you have any question, post a comment below to ask us anytime you want.

The project has been being used in this tutorial was taken from our previous tutorial. You can check that tutorial out or get only the project from this link.

Let’s begin

 

We are going to make the “pacman” face in this project move around when we press an arrow or A, W D S keys.

 


First thing we need to do is create script to control the moving of object.

There is a folder name Scripts that we already created in the previous tutorial, create a C# Script inside this folder and name it something like MovingObject then open it in Script Editor.

In this script, write some lines of code like this:

//Declaring variables

publicfloatmovingSpeed=3f;

 

// Update is called once per frame

void Update (){

//Get horizontal direction

float x =Input.GetAxis("Horizontal")*movingSpeed*Time.deltaTime;

 

//Get vertical direction

float y =Input.GetAxis("Vertical")*movingSpeed*Time.deltaTime;

 

//Set new position for object

transform.position+=new Vector3(x, y,0);

}

 Now let us explain my code.

First, we deleted the Start() method because we don’t actually need that.

Then we declared a variable name movingSpeed to set the speed of object when moving.

In the update() method, we use two local variables x and y to store the direction of object when moving.

Please notice this:

Input.GetAxis("Horizontal")

This function is used to get the direction of object between left and right. The return value of it is a float number from -1 to 1.

When -1 < x < 0, it’s moving to the left.

When 0 < x < 1, it’s moving to the right.

When x = 0, it’s not moving in horizontal direction.

You can understand the same as Input.GetAxis("Vertical") but for up and down directions.


 

If you want to know where is the “Horizontal” and “Vertical” were declared, and which keys are used to get them.

Go to Edit ->ProjectSetting ->Input

 Now look at the Inspector window


When expanding Horizontal (or Vertical), you can see there is a name field was set as “Horizontal” and the left, right, a and d was set for keys.

You are able to change these settings but we don’t recommend you to do that, just leave it as its default.

 

 

movingSpeed*Time.deltaTime;

If our movingSpeed is 3, this means we want our object to move 3 units per second.

If you don’t multiply with Time.deltaTime, it will move 3 units per frame, then the object will move faster when running on a high performance computer. We don’t want that to happen.

In the last line

transform.position+=new Vector3(x, y,0);

Transform is a component attached to the same object with this script.

This is Transform fromInspector window ofUnity Editor

You can see that Transform has 3 attributes (Position, Rotation, Scale) and in this case, we only need to modify the Position of it.

Position of an object is known as a vector3 in 3 – Dimensions space (3D). It has 3 coordinates x, y and z. In this tutorial, we are working in 2D then we only need x and y, z is always set to 0.

That line at the end means that its position will be set to new position in every frame. But only if we press arrow keys (or a, d, w, s keys), the new Vector3 will be different to Vector zero, then the position of object will be changed.

After you save the script, come back to Unity and attach this script to the object you want to move (pacman object) then press play (the triangle button) to test.

Remember that you can modifymoving speed variable to change the speed of it.

If you leave this variable negative number, your object will move in the opposite direction. Have fun.

 That’s how to make a simple moving for a 2D object.

Notice that there are a lot of ways to make an object move, this is only one of them. You can find out more when learning by yourself. If you have any question, don’t forget to ask us in this tutorial’s comment.

 

Thanks for reading, see you again in our next tutorial.