How to Create a Platformer in Unity: Step-by-Step Guide
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.
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); } } }
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.
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); } } }
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.
/* 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); }
Quick Reference
| Concept | Usage | Notes |
|---|---|---|
| Rigidbody2D | Attach to player for physics | Controls movement and collisions |
| BoxCollider2D | Attach to player and platforms | Detects collisions |
| Input.GetAxis("Horizontal") | Reads left/right input | Returns -1 to 1 |
| Input.GetButtonDown("Jump") | Detects jump press | Use with ground check |
| Physics2D.OverlapCircle | Checks if player is on ground | Use with ground layer mask |