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
- Open Unity Hub and create a new project.
- Select the 2D template.
- Name it 2D-Movement and click Create project.
2. Add a player sprite
- In the Hierarchy, right-click > Create Empty and rename it Player.
- Add a Sprite Renderer component via Add Component.
- Assign any square sprite (or a built-in sprite) to the Sprite Renderer.
3. Add a Rigidbody2D
- With the Player selected, click Add Component.
- Search for Rigidbody2D and add it.
- Set Gravity Scale to
0if you only want top-down movement, or keep it at1for platformer-style gravity.
4. Create the movement script
- In the Project window, create a new folder called Scripts.
- Inside it, create a new C# script called PlayerMovement.
- 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
- Drag the PlayerMovement script onto your Player object.
- In the Inspector, set Move Speed to something like
5.
6. Test the game
- Press Play.
- 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.