The sleek, white 3D Unity cube logo is centered on a vibrant blue and purple gradient background

How to Build Your First 2D Game in Unity: A Step-by-Step Tutorial

How to Build Your First 2D Game in Unity: A Step-by-Step Tutorial

Build a complete 2D platformer in Unity with tilemaps, collectibles, enemy AI, and full code samples

21 MAY 2026, 08:13 AM

Highlights

  • Unity-powered 2D platformers remain one of the lowest-risk entry points for beginner game developers in 2026.
  • The tutorial covers complete 2D platformer creation in Unity, from sprite setup and tilemaps to enemy AI and pause systems.
  • The project ends with a fully exportable game build for platforms such as Windows, Mac, and WebGL.

The indie game industry is shrinking its ambitions on purpose. Unity's 2026 Game Development Report, drawn from nearly 5M engine users in 2025, found that over half of surveyed studios are now "focusing on smaller, more manageable projects" as a deliberate strategy against risk. The 2D platformer sits at the center of that shift: high teachability, low scope, and a genre with a proven commercial ceiling.

Unity powered 51% of all games released on Steam in 2024. It also controls roughly 90% of the mobile 2D market, which makes this Unity 2D tutorial the most direct line between zero experience and a game file you can actually ship.

What follows is a complete build walkthrough covering sprite setup, tilemaps, player physics, enemy AI, collectibles, a main menu, a score system, and a pause screen. Every step includes the code.

By the last section, you will have a working 2D platformer in Unity ready for export.

Setting Up Your Unity Beginner Project the Right Way

Download Unity Hub from unity.com, install Unity 6 (released October 2024, currently the stable version), and pair it with Visual Studio 2022 or VS Code. Inside Unity Hub, create a new project using the 2D (URP) template. Name it ‘My2DPlatformer.’

The universal render pipeline (URP) template matters. It replaces the old built-in renderer with one optimized for performance across mobile and desktop, which is where 2D platformer Unity projects most often end up. Once the editor loads, create this folder structure in the Project window: 'Assets/Sprites,' ‘Assets/Scripts,’ ‘Assets/Tilemaps,’ ‘Assets/Prefabs,’ and ‘Assets/Scenes.’

Organizing folders from day one prevents the file chaos that stalls most beginner Unity projects before they reach the finish line.

How to Import and Slice Sprites for a 2D Platformer in Unity

Drag your sprite sheet PNG into ‘Assets/Sprites.’ Select it in the Inspector and configure the following:

  • Texture Type to Sprite (2D and user interface (UI)), Sprite Mode to Multiple, Pixels Per Unit to 32, and Filter Mode to Point (no filter).
  • Click Apply.
  • Open the Sprite Editor, choose Slice, set Grid By Cell Size to your tile dimensions (32x32 is standard), slice, and apply again.
  • Unity now treats each frame as a standalone sprite asset. Free sprite sheets for practice are available via OpenGameArt and itch.io's free assets section.

Building Player Movement: The Core of Every Unity 2D Tutorial

Create a Player GameObject via the Hierarchy, add a Rigidbody 2D and Capsule Collider 2D, set Collision Detection to Continuous, and freeze the Z rotation. Add a child empty object called ‘GroundCheck’ positioned at the player's feet.

Create a new layer named ‘Ground’ under Edit, Project Settings, Tags and Layers, and assign it to every platform and floor object in the scene.

Create ‘PlayerController.cs’ in ‘Assets/Scripts’:

csharp

using UnityEngine;

public class PlayerController : MonoBehaviour

{

    public float moveSpeed = 6f;

    public float jumpForce = 12f;

    public Transform groundCheck;

    public float groundCheckRadius = 0.2f;

    public LayerMask groundLayer;

    private Rigidbody2D rb;

    private float coyoteTimeCounter;

    private float jumpBufferCounter;

    void Start() { rb = GetComponent<Rigidbody2D>(); }

    void Update()

    {

        bool isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);

        coyoteTimeCounter = isGrounded ? 0.15f : coyoteTimeCounter - Time.deltaTime;

        jumpBufferCounter = Input.GetButtonDown("Jump") ? 0.1f : jumpBufferCounter - Time.deltaTime;

        if (coyoteTimeCounter > 0f && jumpBufferCounter > 0f)

        {

            rb.linearVelocity = new Vector2(rb.linearVelocity.x, jumpForce);

            jumpBufferCounter = 0f;

        }

        if (Input.GetButtonUp("Jump") && rb.linearVelocity.y > 0f)

            rb.linearVelocity = new Vector2(rb.linearVelocity.x, rb.linearVelocity.y * 0.5f);

    }

    void FixedUpdate()

    {

        float h = Input.GetAxisRaw("Horizontal");

        rb.linearVelocity = new Vector2(h * moveSpeed, rb.linearVelocity.y);

        if (h != 0) transform.localScale = new Vector3(Mathf.Sign(h), 1f, 1f);

    }

}

In the inspector, drag the ‘GroundCheck’ child object into the Ground Check field and set the Ground Layer mask to the ‘Ground’ layer you created. Without these assignments, the script will throw a null reference error on the first play.

Coyote time gives the player a short grace window to jump after stepping off a ledge. Jump buffering registers the input slightly before landing. Both details are what separate a Unity 2D project that feels responsive from one that feels broken. The Unity Manual on Rigidbody 2D covers additional physics properties worth exploring once this base controller is working.

Designing Levels Fast with Unity Tilemap

Go to Hierarchy, right-click, choose 2D Object, Tilemap, and then Rectangular.

Unity generates a Grid parent with a Tilemap child. Rename the Tilemap ‘Ground,’ add a Tilemap Collider 2D and a Composite Collider 2D, set the auto-generated Rigidbody 2D to Static, and check Used By Composite on the Tilemap Collider. Set the layer to ‘Ground.’

Open the Tile Palette window under Window, 2D, Tile Palette. Create a new palette called ‘LevelTiles,’ save it to ‘Assets/Tilemaps,’ drag your sliced sprites in, and paint the level directly in the Scene view. Use separate Tilemap layers for hazards and decorative elements to keep collision logic clean. Unity's official Tilemap documentation covers isometric and hexagonal grid variants for when this rectangular workflow becomes too familiar.

Adding Enemy AI Patrol to Your 2D Platformer Unity Project

Create an Enemy prefab with a Sprite Renderer, Rigidbody 2D (gravity on, Z rotation frozen), and Box Collider 2D. Add a second Box Collider 2D to the Enemy, check Is Trigger on it, and use that collider for damage detection while keeping the first one for physical collision.

Place two empty GameObjects, ‘PointA’ and ‘PointB,’ at each end of the patrol route, with PointB positioned to the right of PointA.

Create ‘EnemyPatrol.cs’:

csharp

using UnityEngine;

public class EnemyPatrol : MonoBehaviour

{

    public Transform pointA, pointB;

    public float speed = 2f;

    private Transform target;

    private SpriteRenderer sr;

    void Start() { target = pointB; sr = GetComponent<SpriteRenderer>(); }

    void Update()

    {

        transform.position = Vector3.MoveTowards(transform.position,

            new Vector3(target.position.x, target.position.y, transform.position.z),

            speed * Time.deltaTime);

        if (Vector2.Distance(transform.position, target.position) < 0.1f)

            target = (target == pointA) ? pointB : pointA;

        sr.flipX = (target == pointB);

    }

}

For collision damage, add an ‘OnTriggerEnter2D’ check on the enemy that calls your player damage method when the collider tag reads ‘Player.’ The Unity Learn Platformer Microgame is a useful reference for how Unity's own team structures enemy interactions in a comparable project.

Collectibles, Score UI, and Scene Management

Create a Coin prefab with a Sprite Renderer and a Circle Collider 2D set to ‘Is Trigger.’ The Coin.cs script calls ‘ScoreManager.Instance.AddScore(10)’ on trigger entry, then destroys itself. The ScoreManager is a persistent singleton using ‘DontDestroyOnLoad’ that writes the current total to a TextMeshPro UI element anchored to the top right of a Canvas.

TextMeshPro is included by default in Unity 6 and requires no separate installation.

For the main menu, create a new scene named ‘MainMenu,’ add a Canvas with a Start button, and wire it to ‘SceneManager.LoadScene("GameScene")’ via a ‘MainMenu.cs’ script. Add both scenes to File, Build Settings: ‘MainMenu’ at index 0, ‘GameScene’ at index 1.

Pausing works by setting ‘Time.timeScale’ to zero, which stops physics and ‘FixedUpdate,’ and returns ‘Time.deltaTime’ as zero, without touching the scene. A PauseManager.cs script listens for the Escape key, toggles a pause panel GameObject active state, and flips the timeScale between 0 and 1. Any UI that must stay live during a pause should use ‘Time.unscaledDeltaTime’ rather than ‘Time.deltaTime.’

The Unity Scripting Reference for Time.timeScale details edge cases, including how audio sources behave when the scale hits zero.

Build, Export, and What to Build Next

Open Build Settings, confirm scene order, select your platform (Windows, Mac, or WebGL), and click Build. Unity 6 WebGL produces a build folder that you compress into a ZIP file and upload to itch.io, putting your Unity beginner project in front of players without requiring a download.

When uploading, set the project to HTML in the Kind of project dropdown and ensure the ZIP contains the index.html file at the root level, not inside a subfolder; otherwise the game will not load.

Three extensions worth attempting after the base game is complete:

  • Animated characters: Use the Animator window with blend trees to switch between idle, run, and jump states driven by Rigidbody velocity values.
  • Moving platforms: Adapt the patrol script to platforms, then use ‘transform.SetParent()’ to attach the player to the platform on contact and detach on exit. Note that the platform should have a uniform scale to avoid unintended distortion on the player.
  • Save system: Start with PlayerPrefs for high score persistence, then graduate to ‘JsonUtility’ serialization for full level progress saves across sessions.

Why a 2D Platformer is the Smartest First Project in 2026

The game engine market is valued at $3.2B in 2024 and is forecast to reach $13.8B by 2034, growing at roughly 16% annually. Unity sits at the center of that growth with seven million registered developers, 70% of whom work alone or in small teams.

The 2D platformer Unity workflow covered in this tutorial maps directly onto the industry's current direction: reusable systems, contained scope, and a project small enough to ship.

A Unity 2D tutorial that ends with a working, exportable game is not just an exercise in learning code. It is the beginning of a development portfolio, and that portfolio starts with exactly this kind of project.

Probaho Santra

Probaho Santra

Author

Probaho Santra is a content writer at Outlook India with a master’s degree in journalism. Outside work, he enjoys photography, exploring new tech trends, and staying connected with the esports world.

Published At: 21 MAY 2026, 08:13 AM
Tags:Careers