Overview
Unity includes dedicated tools and a physics engine (Box2D) for 2D game development. While 3D physics use Rigidbody and BoxCollider, 2D physics use specialized components ending in 2D: Rigidbody2D and Collider2D.
3D and 2D physics components don't interact with each other—they operate completely independently. Therefore, when developing 2D games, you must use 2D components. Accidentally adding 3D components is a common beginner mistake.
This article covers the basics of Rigidbody2D, the heart of 2D physics, and Collider2D for defining collision shapes.
Rigidbody2D: The Core of 2D Physics
Rigidbody2D is the component that places a game object under the 2D physics engine's control. Adding it enables objects to respond to gravity, move according to forces, and collide with other objects.
Key Differences from Rigidbody
- 2D only: No movement or rotation on the Z axis. All physics calculations are confined to the XY plane.
- Rotation: While 3D uses
Quaternionfor rotation, 2D only rotates around the Z axis, so it's handled as a simplefloat(angle).
Body Type
Rigidbody2D has three Body Type options that significantly affect object behavior:
- Dynamic: The default type. Fully obeys physics laws (forces, gravity, collisions). Use for player characters, thrown balls, and other moving objects.
- Kinematic: Unaffected by physical forces (gravity, collision rebounds), but can be moved via script using
velocityorMovePosition(). Can push otherDynamicobjects and trigger collision events. Use for moving platforms or enemies requiring precise script control. - Static: Completely immune to physics—fixed in space. Similar to
Kinematicbut not intended to be moved by script. Use for ground, walls, and completely immobile objects. Most performant.
Collider2D: Defining Collision Shapes
Collider2D defines an object's physical shape. The physics engine uses this shape for collision detection—not the visible Sprite shape, but the Collider2D shape is the physical entity.
Main Collider2D Types
BoxCollider2D: Rectangular shape. Most common and performant. Widely used for walls, floors, and basic character hitboxes.CircleCollider2D: Circular shape. Suitable for balls and rounded characters. Advantage: collision shape doesn't change when rotating.CapsuleCollider2D: Capsule (rounded rectangle) shape. Very popular for player character hitboxes. No corners means less catching on slopes and small ledges.PolygonCollider2D: Automatically generates polygon collision matching the Sprite shape. Use for complex terrain or objects needing precise hitboxes, but high vertex counts impact performance.CompositeCollider2D: Combines multipleCollider2Dinto one for complex shapes or performance optimization. For example, merging manyBoxCollider2Dfrom a tilemap into one.
Is Trigger
Enabling a Collider2D's Is Trigger checkbox prevents physical collisions (rebounds, push-outs). Instead, it fires trigger events (OnTriggerEnter2D, OnTriggerExit2D) when overlapping other colliders.
Use for item pickup zones or sensors detecting when entering specific areas—cases where you only need overlap detection, not physical responses.
Detecting Collision Events
When objects with Rigidbody2D collide with Collider2D, you can handle events in scripts.
OnCollisionEnter2D(Collision2D collision): Called when a physical collision begins.OnTriggerEnter2D(Collider2D other): Called when another collider enters a trigger.
using UnityEngine;
public class PlayerController2D : MonoBehaviour
{
// Detect physical collisions
private void OnCollisionEnter2D(Collision2D collision)
{
// If the collided object has "Enemy" tag
if (collision.gameObject.CompareTag("Enemy"))
{
Debug.Log("Hit an enemy!");
}
}
// Detect trigger events
private void OnTriggerEnter2D(Collider2D other)
{
// If the entering object has "Coin" tag
if (other.CompareTag("Coin"))
{
Debug.Log("Got a coin!");
Destroy(other.gameObject); // Remove the coin
}
}
}
Summary
Understanding 2D physics is key to creating engaging 2D games.
- Always use
Rigidbody2DandCollider2Dfor 2D games. - Set
Rigidbody2D'sBody Type(Dynamic, Kinematic, Static) appropriately for each object's role. - Use
Collider2Dto define physical shapes. Simpler shapes perform better. - Enable
Is Triggerwhen physical responses aren't needed.
Combining these components and handling OnCollisionEnter2D and OnTriggerEnter2D events in scripts lets you implement core game interactions: character jumping, item collection, enemy combat, and more.