[Unity] 2D Character Movement with Arrow Keys (Beginner Tutorial)

In this guide, we’ll walk through how to control a 2D game character using the keyboard’s arrow keys.

Two Movement Methods

There are two main ways to move a character:

  1. Moving the Transform’s position directly
  2. Using the physics engine

In this tutorial, we’ll focus on the physics engine method, which is ideal for 2D action games because it makes setting up collision detection with obstacles much easier.

Setting Up the Stage and Character

Let’s place the stage and the character in the scene.

For this article, we’ll be using the demo scene from this free asset.

Simple 2D Platformer Assets Pack

When you open the demo scene, the character and stage will be displayed in the Game view.

Character Setup

Attach a Rigidbody 2D component to the character you want to move.

To prevent unwanted rotation, check Freeze Rotation > Z in the Rigidbody 2D settings.

If you don’t check this box, your character might topple over!

Collision Detection

Make sure to add Colliders to both the character and the stage.

Colliders define the areas where objects detect collisions with each other.

If the stage doesn’t have a collider, the player will pass right through it.

Movement Script

Create the following script and attach it to your character.

The script gets the Rigidbody2D component and sets the movement direction and speed when the left or right arrow keys are pressed.

By changing rigidbody2D.velocity, the character moves accordingly.

You can adjust the player’s movement speed by changing the speed value in the Inspector.

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

public class MovePlayer : MonoBehaviour
{
    public float speed = 3f;         // Movement speed of the player
    private float playerSpeed;       // Current horizontal speed

    Rigidbody2D rigidbody2D;         // Reference to the Rigidbody2D component

    // Start is called before the first frame update
    void Start()
    {
        // Get the Rigidbody2D component attached to this GameObject
        rigidbody2D = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        // Move left when Left Arrow key is pressed
        if (Input.GetKey(KeyCode.LeftArrow)) playerSpeed = -speed;
        // Move right when Right Arrow key is pressed
        else if (Input.GetKey(KeyCode.RightArrow)) playerSpeed = speed;
        // Stop when no key is pressed
        else playerSpeed = 0;

        // Set the Rigidbody2D's velocity based on playerSpeed, keeping the existing vertical velocity
        rigidbody2D.velocity = new Vector2(playerSpeed, rigidbody2D.velocity.y);
    }
}

When you start the game, pressing the left and right arrow keys moves the character!

However, since jumping isn’t implemented yet, the character will just fall down.

Conclusion

We covered how to move a 2D character using Unity’s physics engine.

Don’t forget to set up collision detection when moving your character.

For easily creating 2D action games, this popular asset is highly recommended:

Corgi Engine – 2D + 2.5D Platformer