Unity 2D Character & Object Movement System (Transform Translate)


Hello friends, in this post I will talk about how we can move a character or objects while making a 2D game with Unity, and then talk about how we can perform these movements with keys. Without further ado, let's get into our article.


How Can We Move Objects?

Actually, before explaining this, let me give a little explanation: There are many ways to move a character in Unity, but in this article I'll explain moving it with the Translate command. In the future, I'll also explain how to move with different commands and discuss the differences between them.

The command we will use here is "transform.Translate(x,y,z);" what does this mean? With the Translate command, we can change the position of our character. Translate allows us to make calculations based on the current universal position, so we encounter fewer errors.

What you need to know before using the command:

  • We need to know where the x, y, and z axes are. (X is right and left, Y is vertical, Z is depth.)
  • What is Time.DeltaTime for? We need to know this because we use it in the command, and trying to understand with incomplete knowledge will make it harder to grasp the logic.

Now, create a script and name it "character_movement".


Create a function inside and name the function "characterMovement".


Now we will write our codes here. First, let's set a movement speed as a float

float hareket_hizi = 5f;

That's how I set my movement speed.
Now, since I only want this movement command to run on the object where the script is, I will write my movement command with the this command.

this.transform.Translate(0,0,0);

We write our command like that. Now the only thing we need to do is to change the 0,0,0 according to the direction we want it to move, using the movement speed, and multiply by Time.DeltaTime.

Note: The reason for multiplying by Time.DeltaTime is to ensure that it moves at a constant rate and covers equal distance even if the computer lags or freezes.

If we want it to move to the right.

this.transform.Translate(hareket_hizi * Time.deltaTime,0,0);


We make it continuously move to the right on the x axis, you can think of it as moving 5 units per second to the right. If you wanted it to go left, all you need to do is make "hareket_hizi" negative.

this.transform.Translate(-(hareket_hizi * Time.deltaTime),0,0);


You can move it to the left and right like this. Of course, you must not forget to call this function inside any update function.

All the code for moving the object steadily to the left is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class karakter_hareket : MonoBehaviour
{

    void Update()
    {
        karakterHareket();
    }

    void karakterHareket()
    {
        float hareket_hizi = 5f;
        this.transform.Translate(-(hareket_hizi * Time.deltaTime),0,0);
    }
}


Moving the Object According to Clicked Buttons

We learned how to move an object steadily. Now let's go a little further and talk about how we can move it in the direction the user wants.

Let me explain briefly how we will do it. Actually, what we will do is very simple...
First, we will write the codes for the directions we want it to go, and then use if and else to make the object move according to the buttons the user clicks.

To detect which button the user presses, we use commands like Input.GetKey, GetKeyDown, GetKeyUp, and run the command when the key pressed matches the key we want.
Since we want it to work as long as the key is held, we will use GetKey.

GetKey : Works as long as the key is held. (Continuous)
GetKeyDown : Works when the key is pressed. (Once)
GetKeyUp : Works once when the finger is released from the keyboard. (Once)

Now that we've talked about what each of the commands in the code generally does, let's put our codes.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class karakter_hareket : MonoBehaviour
{

    void Update()
    {
        karakterHareket();
    }

    void karakterHareket()
    {
        float hareket_hizi = 5f;

        if (Input.GetKey(KeyCode.A))
        {
            // Actions when A key is pressed.
            this.transform.Translate(-(hareket_hizi * Time.deltaTime),0,0);
            // We made the character move left.
        }else if (Input.GetKey(KeyCode.D))
        {
                // Actions when D key is pressed.
                this.transform.Translate(hareket_hizi * Time.deltaTime, 0, 0);
                // We made the character move right.
        }
        else if (Input.GetKey(KeyCode.W))
        {
            // Actions when W key is pressed.
            this.transform.Translate(0, hareket_hizi * Time.deltaTime, 0);
            // We made the character move up.
        }
        else if (Input.GetKey(KeyCode.S))
        {
            // Actions when S key is pressed.
            this.transform.Translate(0, -(hareket_hizi * Time.deltaTime), 0);
            // We made the character move down.
        }
    }
}


The most important point here is not to mix up the x, y, and z axes and to be able to use the movement commands according to them.