0
0
UnityHow-ToBeginner ยท 4 min read

How to Make a 2D Game in Unity: Step-by-Step Guide

To make a 2D game in Unity, start by creating a new 2D project and importing your sprites. Use GameObjects with SpriteRenderer components to display images, then add Rigidbody2D and Collider2D for physics. Finally, write simple C# scripts to control player movement and game logic.
๐Ÿ“

Syntax

Unity uses C# scripts attached to GameObjects to control game behavior. Key components for 2D games include:

  • SpriteRenderer: Displays 2D images (sprites).
  • Rigidbody2D: Adds physics like gravity and movement.
  • Collider2D: Detects collisions between objects.
  • MonoBehaviour: Base class for scripts to handle game logic.

Scripts use methods like Update() for frame-by-frame actions and FixedUpdate() for physics updates.

csharp
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 5f;
    private Rigidbody2D rb;

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

    void FixedUpdate()
    {
        float moveX = Input.GetAxis("Horizontal");
        float moveY = Input.GetAxis("Vertical");
        Vector2 movement = new Vector2(moveX, moveY);
        rb.velocity = movement * speed;
    }
}
๐Ÿ’ป

Example

This example shows a simple player controller script that moves a 2D character using arrow keys or WASD. Attach this script to a GameObject with a Rigidbody2D component to see the player move smoothly.

csharp
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 5f;
    private Rigidbody2D rb;

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

    void FixedUpdate()
    {
        float moveX = Input.GetAxis("Horizontal");
        float moveY = Input.GetAxis("Vertical");
        Vector2 movement = new Vector2(moveX, moveY);
        rb.velocity = movement * speed;
    }
}
Output
The player GameObject moves in 2D space responding to arrow keys or WASD input.
โš ๏ธ

Common Pitfalls

Common mistakes when making 2D games in Unity include:

  • Forgetting to add a Rigidbody2D component, so physics and movement don't work.
  • Using Update() to change physics properties instead of FixedUpdate(), causing jittery movement.
  • Not setting the SpriteRenderer properly, resulting in invisible sprites.
  • Ignoring collision layers, which can cause unexpected collisions or no collisions.
csharp
/* Wrong: Changing physics velocity in Update() causes inconsistent movement */
void Update()
{
    rb.velocity = new Vector2(1, 0) * speed;
}

/* Right: Use FixedUpdate() for physics changes */
void FixedUpdate()
{
    rb.velocity = new Vector2(1, 0) * speed;
}
๐Ÿ“Š

Quick Reference

Here is a quick cheat sheet for making 2D games in Unity:

ConceptPurposeUsage Tip
SpriteRendererShows 2D imagesAssign sprite in Inspector to display character or object
Rigidbody2DAdds physics and movementUse FixedUpdate() to change velocity or forces
Collider2DDetects collisionsMatch collider shape to sprite for accurate collisions
MonoBehaviourBase script classUse Update() for input, FixedUpdate() for physics
Input.GetAxisReads player inputUse "Horizontal" and "Vertical" for arrow keys/WASD
โœ…

Key Takeaways

Start a new 2D Unity project and import your sprites to use as visuals.
Attach SpriteRenderer, Rigidbody2D, and Collider2D components to GameObjects for display and physics.
Write C# scripts inheriting from MonoBehaviour to control player movement and game logic.
Use FixedUpdate() for physics-related code to ensure smooth movement.
Test frequently in the Unity Editor to catch common setup mistakes early.