Unity 2D: Move a Player with Arrow Keys

This quick tutorial shows how to move a 2D player left and right using the arrow keys (or A/D) in Unity.

1. Create a new 2D project

  1. Open Unity Hub and create a new project.
  2. Select the 2D template.
  3. Name it 2D-Movement and click Create project.

2. Add a player sprite

  1. In the Hierarchy, right-click > Create Empty and rename it Player.
  2. Add a Sprite Renderer component via Add Component.
  3. Assign any square sprite (or a built-in sprite) to the Sprite Renderer.

3. Add a Rigidbody2D

  1. With the Player selected, click Add Component.
  2. Search for Rigidbody2D and add it.
  3. Set Gravity Scale to 0 if you only want top-down movement, or keep it at 1 for platformer-style gravity.

4. Create the movement script

  1. In the Project window, create a new folder called Scripts.
  2. Inside it, create a new C# script called PlayerMovement.
  3. Double-click to open it in your code editor and replace the contents with the script below.
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float moveSpeed = 5f;

    Rigidbody2D rb;
    Vector2 movement;

    void Awake()
    {
        rb = GetComponent();
    }

    void Update()
    {
        // Get raw input (-1, 0, 1)
        float inputX = Input.GetAxisRaw("Horizontal");
        float inputY = Input.GetAxisRaw("Vertical");

        movement = new Vector2(inputX, inputY).normalized;
    }

    void FixedUpdate()
    {
        rb.velocity = movement * moveSpeed;
    }
}

5. Attach the script

  1. Drag the PlayerMovement script onto your Player object.
  2. In the Inspector, set Move Speed to something like 5.

6. Test the game

  1. Press Play.
  2. Use the arrow keys or WASD to move the player around the scene.

You now have a simple 2D player controller that you can expand with animations, collisions, and more.