Move a 2d object in unity

This blog aims to teach how to move a 2D object in unity, so we will go step by step.

Create the object that we are going to move

The first thing we are going to do is create our object to move. To do this, right click in the hierarchy window, look for the 3D object option and choose cube.

You can do it with the sprites you already have or with any object that you want to move in 2 dimensions. Just for the example we will use the cube.

Add the Rigidbody2D component

The second thing is to add the physics component for 2D objects called Rigidbody2D. For this, with the object selected in our hierarchy, we will go to the inspector and at the bottom we will find the Add Component button, we will left click and we will look for the name of the component.

We will see how the component already added appears in the inspector of our object.

Adjusting the Rigidbody 2D

For the purpose of our blog we will set gravity to 0 in order to prevent our object from falling infinitely.

However, the value of gravity depends on the project you want to create.

Create a script to move your object

We will go to the project window and right click on it, in the create option we will go C # script

A script will be created, we can assign the name we want (remember to put a descriptive name of the script so when you have many scripts you will not be confused)

Let’s define Horizontal and Vertical

The first thing we will do is define how we will access the vertical and horizontal movements of our object.

We create some float variables called Vertical and Horizontal

Then using the void UpDate method we can update our vertical and horizontal inputs for each frame.

In order to do that we must access the Unity input manager and we do it with the Input.GetAxis.

The string that it requests is the name that the X and Y axes receive in the Unity Input manager, so they are “Horizontal” and “Vertical”.

Define velocity and Rigidbody 2D in script

Then, for an object to move, it requires a value that corresponds to the speed at which it will move, so we create a variable of type float call velocidad.

we need to access the position of our object and we can do that through the rigidbody2D.

Let’s define the new position

In order to make our object move we must create a Vector2 that for the example we will call New_position for that we will take the current position of our object and we will add a new vector that will depend on the speed and the input of vertical or horizontal movement.

This new vector will also be dependent on something called Time.deltatime which will normalize our movement in such a way that it looks smoother.

To end …

We save our Script with Ctrl + S (If you don’t save it, the changes will not be seen in Unity).

We go back to Unity and add our Script to the object we want to move (we do it like a component).

Enter play mode and it is ready!!!

Our object will be moved with the AWSD keys and the arrows.

If you find this blog useful let us a comment, and follow us in our instagram @tortugamedev and twitter @Totuas2.

2 thoughts on “Move a 2d object in unity”

  1. Pingback: Unity: An example of trigger actions between two game objects using a Collider 2D - Tortua Production

  2. Pingback: Unity: Un ejemplo de como disparar acciones entre dos objetos usando Collider2D - Tortua Production

Leave a Comment

Your email address will not be published.