Introduction
Physics is the backbone of most 2D games — from platformers and puzzles to shooters and endless runners. Whether you’re dropping objects, bouncing balls, or simulating rope swings, Unity’s 2D physics engine makes it all possible.
This guide will walk you through everything you need to know about Unity 2D physics, including Rigidbody2D, colliders, joints, and collision handling — with practical examples and performance tips to help you make your games feel natural, responsive, and fun.
1. Understanding Unity’s 2D Physics System
Unity 2D physics is built on Box2D, a powerful physics engine designed specifically for 2D games. It handles:
- Gravity
- Collisions
- Forces (like impulse or torque)
- Constraints (like joints or limits)
When used correctly, Unity physics makes your objects behave like they exist in a real world — falling, bouncing, colliding, or rolling naturally.
2. Rigidbody2D – The Heart of 2D Physics
A Rigidbody2D component gives your game object physical behavior — it allows the object to move and react to forces, gravity, and collisions.
Without it, your object is just static — it won’t move no matter what happens.
A. Rigidbody2D Body Types
Unity offers three main Rigidbody2D body types:
Dynamic:
- Reacts to physics forces and collisions.
- Best for movable objects like players, enemies, or crates.
Kinematic:
- Ignores gravity and forces.
- Moved manually via code (e.g., moving platforms).
- Static:
- Doesn’t move or react to forces.
- Perfect for ground, walls, and obstacles.
B. Common Rigidbody2D Properties
- Property
- Description
Mass
Controls how heavy the object is.
Gravity Scale
Adjusts how strongly gravity affects it.
Linear Drag
Slows movement over time.
Angular Drag
Slows down rotation.
Constraints
Lock position or rotation axes.
⚙️ Pro Tip: If your player keeps sliding on flat ground, increase the Linear Drag slightly and reduce Gravity Scale to make movement smoother.
3. Colliders – Detecting Contact Between Objects
Colliders define the shape of your object for collision detection. Unity offers several 2D collider types:
A. Basic 2D Colliders
Box Collider 2D – Simple rectangle (perfect for tiles, platforms).
Circle Collider 2D – Great for round objects like balls or coins.
Capsule Collider 2D – Ideal for characters that move and jump.
Polygon Collider 2D – Custom shapes for complex sprites.
Edge Collider 2D – For drawing custom edges like terrain or slopes.
🎯 Keep colliders simple. The more complex your colliders, the more CPU time Unity needs for physics calculations.
B. IsTrigger Option
If you check Is Trigger, the collider won’t physically block other objects — instead, it detects overlap events using:
void OnTriggerEnter2D(Collider2D col) { Debug.Log("Trigger hit: " + col.name); }
Use triggers for pickups, checkpoints, or detecting entry zones.
4. Handling Collisions in Unity 2D
Unity provides easy ways to detect collisions and respond to them through scripts.
A. Collision Events
Add one of these methods inside your script:
void OnCollisionEnter2D(Collision2D collision) { Debug.Log("Collided with: " + collision.gameObject.name); } void OnCollisionStay2D(Collision2D collision) { Debug.Log("Still colliding with: " + collision.gameObject.name); } void OnCollisionExit2D(Collision2D collision) { Debug.Log("Stopped colliding with: " + collision.gameObject.name); }
đź’ˇ These methods only work if at least one object has a Rigidbody2D, and both have colliders.
B. Collision Layers
You can define which objects can collide in Unity’s Physics 2D Settings → Layer Collision Matrix.
This is extremely useful for avoiding unnecessary collision checks (e.g., bullets colliding with other bullets).
5. Applying Forces and Movement
Instead of using transform.position to move physics objects, use Rigidbody2D’s built-in methods.
A. Adding Force
rb.AddForce(Vector2.right * 10f, ForceMode2D.Impulse);
This simulates a burst of movement (like a jump or kick).
B. Setting Velocity
rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
Use this for character movement — it’s predictable and frame-rate independent.
C. Adding Torque (Rotation)
rb.AddTorque(5f);
Spins the object clockwise or counterclockwise.
⚙️ Pro Tip: Don’t mix transform manipulation (transform.position) with Rigidbody2D movement — it breaks physics and causes jitter.
6. Joints in Unity 2D
Joints connect two Rigidbody2D objects together with specific movement rules. They’re essential for creating realistic physics interactions like ropes, doors, or chains.
Common 2D Joint Types
- Joint
- Description
- Fixed Joint 2D
- Keeps two bodies locked together.
- Hinge Joint 2D
- Lets one body rotate around another (like a door hinge).
- Spring Joint 2D
- Adds a spring-like connection with elasticity.
- Distance Joint 2D
- Keeps two bodies a fixed distance apart.
- Slider Joint 2D
- Restrains movement along a line (like an elevator).
- Wheel Joint 2D
- Simulates wheel suspension or car physics.
Example:
HingeJoint2D hinge = gameObject.AddComponent<HingeJoint2D>(); hinge.connectedBody = targetRigidbody;
7. Physics Materials 2D
- A Physics Material 2D controls how objects bounce and slide.
You can create one via Assets → Create → Physics Material 2D. - Friction: Controls sliding resistance.
- Bounciness: Determines how much an object rebounds.
đź§© Example:
- For ice surfaces, use low friction (0.1).
- For bouncy balls, use high bounciness (0.9).
Assign the material to your collider in the Inspector.
8. Performance Tips for 2D Physics
- Physics can be heavy on performance if overused. To keep your game smooth:
- Use simple colliders (Box or Circle) instead of complex Polygon Colliders.
- Disable physics simulation on inactive objects.
- Adjust Fixed Timestep under Project Settings → Time (higher = fewer physics updates).
- Limit collision layers — disable unnecessary layer checks.
- Use Rigidbody2D.Sleep() to stop simulating stationary objects.
- Avoid scaling colliders frequently — it triggers costly recalculations.
9. Debugging and Visualization
- Unity makes it easy to visualize your 2D physics interactions:
- Toggle Gizmos in Scene view to see collider outlines.
- Use Debug.DrawRay() to visualize raycasts.
Check Edit → Project Settings → Physics 2D → Gizmos to color-code collision layers.
For performance analysis, open Window → Analysis → Profiler → Physics 2D.
This shows how much CPU time your collisions and Rigidbody2D updates take.
10. Example: Player Jump and Ground Detection
Let’s combine what we’ve learned into a simple example:
public class PlayerController : MonoBehaviour { public float jumpForce = 7f; private Rigidbody2D rb; private bool isGrounded; void Start() { rb = GetComponent<Rigidbody2D>(); } void Update() { if (Input.GetKeyDown(KeyCode.Space) && isGrounded) { rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse); } } void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Ground")) isGrounded = true; } void OnCollisionExit2D(Collision2D collision) { if (collision.gameObject.CompareTag("Ground")) isGrounded = false; } }
This simple script handles jumping, ground collision, and force-based movement — all powered by Unity’s 2D physics.
11. Common Mistakes to Avoid
❌ Moving Rigidbody2D with transform.position
❌ Mixing 2D and 3D physics components
❌ Ignoring mass and drag settings
❌ Forgetting colliders on physics objects
❌ Too many unnecessary joint connections
12. Final Thoughts
Unity’s 2D physics system is powerful, intuitive, and beginner-friendly. Once you understand how Rigidbody2D, Colliders, and Joints work together, you can simulate almost any kind of physical interaction — from platformer jumps to swinging bridges and destructible environments.
Whether you’re building a Flappy Bird clone, a physics-based puzzle, or a ragdoll playground, mastering Unity’s physics tools will take your 2D game development skills to the next level.
Experiment, tweak values, and always test your game on real devices — because physics that “feels right” is what makes great games truly satisfying.
Tag:
unity 2d physics tutorial, Rigidbody2D, Unity collision system, Unity 2D joints, Unity physics materials, Unity 2D triggers, Unity Rigidbody guide
Comments (0)