How to Build Your First 2D Game in Unity - Complete Beginner Tutorial

Have you ever dreamed of creating your own video game? Unity makes it possible for anyone to build 2D games, even if you've never coded before. In this comprehensive tutorial, you'll learn how to create your first 2D game from scratch using Unity's powerful tools.

By the end of this tutorial, you'll have a complete 2D platformer game that you can play, share, and even publish. We'll cover everything from setting up your project to adding player movement, creating levels, and building your game for distribution.

Why Unity is Perfect for 2D Game Development

Unity has become the go-to engine for 2D game development for several compelling reasons:

Cross-Platform Support: Build once, deploy everywhere. Your 2D game can run on PC, Mac, mobile devices, and even web browsers.

Visual Editor: Unity's intuitive interface lets you design levels visually without writing code for every element.

Rich Asset Store: Access thousands of free and paid assets to accelerate your development.

Strong Community: With millions of developers using Unity, you'll never run out of tutorials, forums, and helpful resources.

C# Programming: Learn a valuable programming language that's used in professional game development.

What You'll Build in This Tutorial

We'll create a classic 2D platformer game featuring:

  • A player character that can move and jump
  • Multiple platforms to jump between
  • Basic physics and collision detection
  • A simple level design
  • Visual polish with sprites and backgrounds
  • A playable game you can build and share

This project will teach you the fundamentals of 2D game development while creating something you can be proud of.

Prerequisites and Setup

Before we begin, you'll need:

Unity Hub and Unity Editor: Download the latest LTS version of Unity (2022.3 or newer) from unity.com.

Basic Computer Skills: You should be comfortable with installing software and navigating folders.

Time Commitment: This tutorial takes a moderate amount of time to complete.

No Programming Experience Required: We'll teach you everything you need to know about C# scripting.

Step 1: Setting Up Your Unity Project

Let's start by creating a new Unity project optimized for 2D development.

Creating a New Project

  1. Open Unity Hub and click "New Project"
  2. Select "2D Core" template
  3. Name your project "MyFirst2DGame"
  4. Choose a location on your computer
  5. Click "Create Project"

Configuring Project Settings

Once your project loads, let's optimize it for 2D development:

  1. Go to Edit > Project Settings
  2. In the XR Plug-in Management section, disable any VR/AR features
  3. In Player Settings, set your target platform (PC, Mac, or WebGL)
  4. Set the default screen resolution to 1920x1080

Setting Up the Scene

Your new project comes with a default scene. Let's prepare it for our game:

  1. In the Hierarchy window, delete the "Main Camera" (we'll add a new one)
  2. Right-click in the Hierarchy and select Create Empty
  3. Rename it to "GameManager"
  4. This will be our main game object

Step 2: Creating Your First Scene

Now let's build the foundation of our game world.

Adding a Camera

  1. Right-click in the Hierarchy and select Camera
  2. Rename it to "Main Camera"
  3. In the Inspector, set the Size to 5 (this controls how much of the scene is visible)
  4. Set the Position to (0, 0, -10)

Creating a Background

  1. Right-click in the Hierarchy and select 2D Object > Sprite
  2. Rename it to "Background"
  3. In the Inspector, set the Scale to (20, 15, 1)
  4. Set the Position to (0, 0, 1)
  5. Change the Color to a light blue (0.7, 0.9, 1, 1)

Adding Ground Platforms

Let's create some platforms for our character to jump on:

  1. Right-click in the Hierarchy and select 2D Object > Sprite
  2. Rename it to "Ground"
  3. Set the Scale to (10, 1, 1)
  4. Set the Position to (0, -4, 0)
  5. Change the Color to brown (0.6, 0.4, 0.2, 1)

Create additional platforms:

  • Platform 1: Position (3, -1, 0), Scale (3, 0.5, 1)
  • Platform 2: Position (-3, 1, 0), Scale (2, 0.5, 1)
  • Platform 3: Position (6, 2, 0), Scale (2, 0.5, 1)

Step 3: Adding a Player Character

Now let's create our main character that players will control.

Creating the Player

  1. Right-click in the Hierarchy and select 2D Object > Sprite
  2. Rename it to "Player"
  3. Set the Scale to (1, 1, 1)
  4. Set the Position to (0, -2, 0)
  5. Change the Color to red (1, 0.2, 0.2, 1)

Adding a Collider

  1. With the Player selected, click Add Component in the Inspector
  2. Select Physics 2D > Box Collider 2D
  3. This will allow the player to collide with platforms

Adding a Rigidbody

  1. With the Player still selected, click Add Component
  2. Select Physics 2D > Rigidbody 2D
  3. In the Rigidbody settings:
    • Set Gravity Scale to 3
    • Set Freeze Rotation Z to true (prevents the player from spinning)

Step 4: Implementing Basic Movement

Now let's add the code that makes our character move and jump.

Creating the Player Controller Script

  1. In the Project window, right-click and select Create > C# Script
  2. Name it "PlayerController"
  3. Double-click to open it in your code editor

Writing the Movement Code

Replace the default code with this:

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5f;
    public float jumpForce = 10f;

    private Rigidbody2D rb;
    private bool isGrounded;

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

    void Update()
    {
        // Horizontal movement
        float horizontalInput = Input.GetAxis("Horizontal");
        rb.velocity = new Vector2(horizontalInput * moveSpeed, rb.velocity.y);

        // Jumping
        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = true;
        }
    }

    void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = false;
        }
    }
}

Attaching the Script

  1. Save the script and return to Unity
  2. Select the Player object
  3. Drag the PlayerController script from the Project window to the Player in the Inspector
  4. You should see the script component with moveSpeed and jumpForce values

Setting Up Ground Tags

  1. Select all your platform objects (Ground, Platform 1, 2, 3)
  2. In the Inspector, find the Tag dropdown
  3. Click Add Tag and create a new tag called "Ground"
  4. Select "Ground" as the tag for all platform objects

Step 5: Adding Platforms and Collisions

Let's enhance our level with more interesting platform layouts.

Creating More Platforms

Add these additional platforms to create a more challenging level:

  • Moving Platform: Position (0, 3, 0), Scale (2, 0.3, 1)
  • High Platform: Position (8, 4, 0), Scale (1.5, 0.3, 1)
  • Low Platform: Position (-6, -1, 0), Scale (1.5, 0.3, 1)

Adding Colliders to All Platforms

  1. Select each platform object
  2. Add a Box Collider 2D component if it doesn't have one
  3. Set the tag to "Ground" for all platforms

Testing Your Movement

  1. Click the Play button in Unity
  2. Use the A and D keys (or arrow keys) to move left and right
  3. Press Space to jump
  4. Try to reach all the platforms!

Step 6: Creating Your First Level

Now let's design a more structured level with a clear goal.

Level Design Principles

Flow: Create a path that guides the player from start to finish Challenge: Gradually increase difficulty with higher platforms Rewards: Add visual elements that make the player want to explore

Building Your Level

  1. Start Platform: Position (-8, -4, 0), Scale (2, 1, 1)
  2. First Jump: Position (-4, -2, 0), Scale (2, 0.5, 1)
  3. Second Jump: Position (0, 0, 0), Scale (2, 0.5, 1)
  4. Third Jump: Position (4, 2, 0), Scale (2, 0.5, 1)
  5. Final Platform: Position (8, 4, 0), Scale (3, 1, 1)

Adding a Goal

  1. Create a new sprite object called "Goal"
  2. Position it at (8, 5, 0) with scale (1, 1, 1)
  3. Color it green (0.2, 1, 0.2, 1)
  4. Add a Box Collider 2D and set it as a trigger
  5. Tag it as "Goal"

Step 7: Adding Visual Polish

Let's make our game look more professional with better visuals.

Improving the Player

  1. Select the Player object
  2. In the Sprite Renderer component, try different shapes:
    • Change the Sprite to a circle or square
    • Adjust the Color to make it more vibrant
    • Add a Sorting Layer to ensure proper rendering order

Enhancing Platforms

  1. Select each platform
  2. Experiment with different colors and shapes
  3. Add slight variations in size to make the level more interesting

Adding a Background

  1. Create a new sprite called "Sky"
  2. Position it at (0, 0, 2) with scale (25, 20, 1)
  3. Color it with a gradient effect (light blue at top, darker at bottom)

Step 8: Testing and Building Your Game

Now let's test our game and prepare it for distribution.

Playtesting Your Game

  1. Click Play in Unity
  2. Test all the movement mechanics
  3. Try to reach the goal platform
  4. Note any issues or improvements needed

Common Issues and Fixes

Player falls through platforms: Check that all platforms have colliders Player can't jump: Ensure the Ground tag is set correctly Camera doesn't follow player: We'll add camera following in the next section

Building Your Game

  1. Go to File > Build Settings
  2. Select your target platform (PC, Mac, or WebGL)
  3. Click Add Open Scenes to include your current scene
  4. Click Build and choose a location to save your game
  5. Unity will create a playable version of your game!

Common Beginner Mistakes to Avoid

Programming Mistakes

Forgetting to save scripts: Always save your C# files before testing Case sensitivity: C# is case-sensitive, so "GetComponent" is different from "getcomponent" Missing semicolons: Every statement in C# must end with a semicolon

Design Mistakes

Making platforms too small: Players need room to land Creating impossible jumps: Test your level design as you build Ignoring the camera: Make sure players can see where they're going

Performance Mistakes

Too many objects: Keep your scene clean and organized Missing colliders: Every interactive object needs a collider Forgetting to optimize: Use Unity's Profiler to check performance

Next Steps and Further Learning

Congratulations! You've built your first 2D game in Unity. Here's how to continue your game development journey:

Immediate Improvements

Add Sound Effects: Import audio files and add them to your player controller Create More Levels: Duplicate your scene and design new challenges Add Enemies: Create moving obstacles that the player must avoid Implement Scoring: Track how long it takes to reach the goal

Advanced Features

Animation: Learn Unity's Animation system to make your character move smoothly Particle Effects: Add visual flair with Unity's Particle System Multiple Scenes: Create a menu system and level selection Save System: Allow players to save their progress

Learning Resources

Unity Learn: Official Unity tutorials and courses Unity Documentation: Comprehensive guides for all Unity features Game Development Communities: Join forums and Discord servers YouTube Channels: Follow game development channels for inspiration

Frequently Asked Questions

Do I need to know programming to use Unity?

While Unity's visual tools are powerful, learning C# programming will unlock much more potential. Start with basic concepts and gradually build your skills.

Can I make money from games I create in Unity?

Yes! Unity's Personal license is free for developers earning under $100,000 per year. Many successful indie games have been made with Unity.

How long does it take to learn Unity?

You can create simple games in a few days, but mastering Unity takes months or years of practice. Start with small projects and gradually increase complexity.

What's the difference between 2D and 3D in Unity?

2D games use sprites and 2D physics, while 3D games use 3D models and physics. Unity can handle both, but this tutorial focuses on 2D development.

Can I use Unity for mobile games?

Absolutely! Unity excels at mobile game development. The same project can be built for iOS, Android, and other platforms.

Conclusion

You've successfully created your first 2D game in Unity! This tutorial covered the essential concepts of 2D game development, from project setup to building a playable game. The skills you've learned here form the foundation for creating more complex games.

Remember, game development is a journey of continuous learning. Start with small projects, experiment with new features, and don't be afraid to make mistakes. Every successful game developer started exactly where you are now.

Ready to take your game development skills further? Check out our Unity Game Development Course for more advanced tutorials and techniques.

Found this tutorial helpful? Share it with other aspiring game developers and bookmark it for future reference. Happy game developing!


This tutorial is part of our comprehensive game development learning series. For more tutorials, guides, and resources, visit our Game Development Hub.