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
- Open Unity Hub and click "New Project"
- Select "2D Core" template
- Name your project "MyFirst2DGame"
- Choose a location on your computer
- Click "Create Project"
Configuring Project Settings
Once your project loads, let's optimize it for 2D development:
- Go to Edit > Project Settings
- In the XR Plug-in Management section, disable any VR/AR features
- In Player Settings, set your target platform (PC, Mac, or WebGL)
- 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:
- In the Hierarchy window, delete the "Main Camera" (we'll add a new one)
- Right-click in the Hierarchy and select Create Empty
- Rename it to "GameManager"
- 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
- Right-click in the Hierarchy and select Camera
- Rename it to "Main Camera"
- In the Inspector, set the Size to 5 (this controls how much of the scene is visible)
- Set the Position to (0, 0, -10)
Creating a Background
- Right-click in the Hierarchy and select 2D Object > Sprite
- Rename it to "Background"
- In the Inspector, set the Scale to (20, 15, 1)
- Set the Position to (0, 0, 1)
- 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:
- Right-click in the Hierarchy and select 2D Object > Sprite
- Rename it to "Ground"
- Set the Scale to (10, 1, 1)
- Set the Position to (0, -4, 0)
- 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
- Right-click in the Hierarchy and select 2D Object > Sprite
- Rename it to "Player"
- Set the Scale to (1, 1, 1)
- Set the Position to (0, -2, 0)
- Change the Color to red (1, 0.2, 0.2, 1)
Adding a Collider
- With the Player selected, click Add Component in the Inspector
- Select Physics 2D > Box Collider 2D
- This will allow the player to collide with platforms
Adding a Rigidbody
- With the Player still selected, click Add Component
- Select Physics 2D > Rigidbody 2D
- 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
- In the Project window, right-click and select Create > C# Script
- Name it "PlayerController"
- 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
- Save the script and return to Unity
- Select the Player object
- Drag the PlayerController script from the Project window to the Player in the Inspector
- You should see the script component with moveSpeed and jumpForce values
Setting Up Ground Tags
- Select all your platform objects (Ground, Platform 1, 2, 3)
- In the Inspector, find the Tag dropdown
- Click Add Tag and create a new tag called "Ground"
- 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
- Select each platform object
- Add a Box Collider 2D component if it doesn't have one
- Set the tag to "Ground" for all platforms
Testing Your Movement
- Click the Play button in Unity
- Use the A and D keys (or arrow keys) to move left and right
- Press Space to jump
- 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
- Start Platform: Position (-8, -4, 0), Scale (2, 1, 1)
- First Jump: Position (-4, -2, 0), Scale (2, 0.5, 1)
- Second Jump: Position (0, 0, 0), Scale (2, 0.5, 1)
- Third Jump: Position (4, 2, 0), Scale (2, 0.5, 1)
- Final Platform: Position (8, 4, 0), Scale (3, 1, 1)
Adding a Goal
- Create a new sprite object called "Goal"
- Position it at (8, 5, 0) with scale (1, 1, 1)
- Color it green (0.2, 1, 0.2, 1)
- Add a Box Collider 2D and set it as a trigger
- Tag it as "Goal"
Step 7: Adding Visual Polish
Let's make our game look more professional with better visuals.
Improving the Player
- Select the Player object
- 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
- Select each platform
- Experiment with different colors and shapes
- Add slight variations in size to make the level more interesting
Adding a Background
- Create a new sprite called "Sky"
- Position it at (0, 0, 2) with scale (25, 20, 1)
- 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
- Click Play in Unity
- Test all the movement mechanics
- Try to reach the goal platform
- 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
- Go to File > Build Settings
- Select your target platform (PC, Mac, or WebGL)
- Click Add Open Scenes to include your current scene
- Click Build and choose a location to save your game
- 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.