0
0
UnityHow-ToBeginner ยท 4 min read

How to Create a Platformer in Unity: Step-by-Step Guide

To create a platformer in Unity, use Rigidbody2D for physics and BoxCollider2D for collision detection on your player and platforms. Write a C# script to handle player movement and jumping by applying forces or changing velocity on the Rigidbody2D component.
๐Ÿ“

Syntax

In Unity, a platformer player typically uses a Rigidbody2D component for physics and a BoxCollider2D for collision detection. Movement is controlled by modifying the velocity property of the Rigidbody2D inside a C# script. Jumping is done by applying an upward force or setting the vertical velocity.

  • Rigidbody2D.velocity: Controls the speed and direction of the player.
  • Input.GetAxis("Horizontal"): Reads left/right input from keyboard or controller.
  • Input.GetButtonDown("Jump"): Detects jump button press.
  • Physics2D.OverlapCircle: Checks if the player is touching the ground.
csharp
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 5f;
    public float jumpForce = 10f;
    public Transform groundCheck;
    public float groundCheckRadius = 0.2f;
    public LayerMask groundLayer;

    private Rigidbody2D rb;
    private bool isGrounded;

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

    void Update()
    {
        float moveInput = Input.GetAxis("Horizontal");
        rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);

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

        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }
    }
}
Output
A player GameObject moves left and right with arrow keys or A/D keys and jumps when pressing the jump button (spacebar) only if touching the ground.
๐Ÿ’ป

Example

This example shows a complete player controller script for a 2D platformer. Attach this script to your player GameObject with a Rigidbody2D and BoxCollider2D. Create an empty child GameObject named groundCheck positioned at the player's feet to detect ground contact. Assign the ground layer in the inspector.

csharp
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 5f;
    public float jumpForce = 10f;
    public Transform groundCheck;
    public float groundCheckRadius = 0.2f;
    public LayerMask groundLayer;

    private Rigidbody2D rb;
    private bool isGrounded;

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

    void Update()
    {
        float moveInput = Input.GetAxis("Horizontal");
        rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);

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

        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }
    }
}
Output
Player moves horizontally and jumps only when on the ground, creating basic platformer controls.
โš ๏ธ

Common Pitfalls

1. Not checking if the player is grounded before jumping: This causes double jumps or flying. Always check ground contact using Physics2D.OverlapCircle or similar.

2. Forgetting to assign the ground layer mask: The ground check won't detect platforms if the layer mask is missing or incorrect.

3. Using transform.position to move the player instead of Rigidbody2D velocity: This breaks physics and collisions.

csharp
/* Wrong: Moving player by changing position directly */
void Update() {
    float moveInput = Input.GetAxis("Horizontal");
    transform.position += new Vector3(moveInput * speed * Time.deltaTime, 0, 0);
}

/* Right: Using Rigidbody2D velocity for smooth physics */
void Update() {
    float moveInput = Input.GetAxis("Horizontal");
    rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}
Output
Wrong approach causes jittery movement and ignores physics; right approach respects collisions and smooth movement.
๐Ÿ“Š

Quick Reference

ConceptUsageNotes
Rigidbody2DAttach to player for physicsControls movement and collisions
BoxCollider2DAttach to player and platformsDetects collisions
Input.GetAxis("Horizontal")Reads left/right inputReturns -1 to 1
Input.GetButtonDown("Jump")Detects jump pressUse with ground check
Physics2D.OverlapCircleChecks if player is on groundUse with ground layer mask
โœ…

Key Takeaways

Use Rigidbody2D and BoxCollider2D components for player physics and collisions.
Control player movement by setting Rigidbody2D.velocity based on input.
Always check if the player is grounded before allowing jumps.
Avoid moving the player by changing transform.position directly to keep physics intact.
Use a ground check with a small circle and layer mask to detect when the player can jump.