The Rigidbody2D component lets objects in a 2D game move and react to forces like gravity and collisions, making them behave like real things.
Rigidbody2D component in Unity
Start learning this pattern below
Jump into concepts and practice - no test required
Rigidbody2D rb = gameObject.AddComponent<Rigidbody2D>(); rb.gravityScale = 1.0f; rb.mass = 1.0f; rb.velocity = new Vector2(2f, 0f);
You add Rigidbody2D to a GameObject to make it affected by 2D physics.
Properties like gravityScale and mass control how it moves and reacts.
Rigidbody2D rb = gameObject.AddComponent<Rigidbody2D>(); rb.gravityScale = 0; // No gravity rb.mass = 2.0f;
Rigidbody2D rb = GetComponent<Rigidbody2D>(); rb.velocity = new Vector2(5f, 0f);
Rigidbody2D rb = GetComponent<Rigidbody2D>(); rb.AddForce(new Vector2(0f, 10f), ForceMode2D.Impulse);
This script adds a Rigidbody2D to the object, so it falls with gravity. When you press space, it pushes the object up like a jump and prints a message.
using UnityEngine; public class SimpleMover : MonoBehaviour { private Rigidbody2D rb; void Start() { rb = gameObject.AddComponent<Rigidbody2D>(); rb.gravityScale = 1.0f; rb.mass = 1.0f; } void Update() { if (Input.GetKeyDown(KeyCode.Space)) { rb.AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse); Debug.Log("Jump force applied!"); } } }
Remember to add a Collider2D component to detect collisions properly with Rigidbody2D.
Setting gravityScale to 0 makes the object ignore gravity but still respond to forces.
Use ForceMode2D.Impulse for instant pushes, and ForceMode2D.Force for continuous forces.
Rigidbody2D makes 2D objects move and react like real things using physics.
You control movement by changing properties or applying forces.
Combine Rigidbody2D with Collider2D for collision detection and response.
Practice
Rigidbody2D component in Unity?Solution
Step 1: Understand Rigidbody2D role
The Rigidbody2D component applies physics to 2D objects, enabling movement and reactions.Step 2: Compare with other options
Options A, B, and D relate to input, sprites, and UI, not physics behavior.Final Answer:
To make 2D objects move and react using physics -> Option AQuick Check:
Rigidbody2D = physics movement [OK]
- Confusing Rigidbody2D with sprite rendering
- Thinking Rigidbody2D handles input
- Mixing Rigidbody2D with UI components
Solution
Step 1: Identify correct component type
Rigidbody2D is the correct physics component for 2D objects, so use AddComponent<Rigidbody2D>().Step 2: Check other options
A adds SpriteRenderer, gameObject.AddComponent<Rigidbody>(); adds 3D Rigidbody, C adds Collider2D, which are different components.Final Answer:
gameObject.AddComponent<Rigidbody2D>(); -> Option BQuick Check:
Add Rigidbody2D with AddComponent<Rigidbody2D>() [OK]
- Using Rigidbody instead of Rigidbody2D
- Adding Collider2D instead of Rigidbody2D
- Confusing SpriteRenderer with Rigidbody2D
Rigidbody2D rb = GetComponent<Rigidbody2D>(); rb.velocity = new Vector2(5, 0); Debug.Log(rb.position);What will be printed in the console immediately after this code runs?
Solution
Step 1: Understand Rigidbody2D velocity effect
Setting velocity changes movement over time, but position updates in the next physics frame, not immediately.Step 2: Analyze Debug.Log timing
Debug.Log prints current position instantly, before Rigidbody2D moves, so it shows the original position.Final Answer:
The current position of the Rigidbody2D before moving -> Option DQuick Check:
Velocity changes future movement, position now = original [OK]
- Assuming position updates immediately after velocity set
- Expecting error when setting velocity directly
- Thinking position always zero
Rigidbody2D rb = GetComponent<Rigidbody2D>(); rb.AddForce(new Vector2(10, 0));But the object does not move. What is the most likely reason?
Solution
Step 1: Check Rigidbody2D Body Type effect
If Body Type is Static, physics forces do not move the object.Step 2: Evaluate other options
Force vector (10,0) is enough; AddForce works with Rigidbody2D; MovePosition is for manual moves, not force.Final Answer:
The Rigidbody2D's Body Type is set to Static -> Option CQuick Check:
Static body ignores forces [OK]
- Using Static body type expecting movement
- Thinking AddForce is invalid for Rigidbody2D
- Confusing MovePosition with AddForce
Solution
Step 1: Choose Rigidbody2D Body Type for physics movement
Dynamic body type allows physics-based movement and velocity control.Step 2: Use velocity for smooth movement and instant stop
Setting rb.velocity directly controls speed and can be set to zero to stop immediately.Step 3: Evaluate other options
Kinematic bodies don't respond to forces; Static bodies don't move; MovePosition is for manual moves, not smooth physics velocity control.Final Answer:
Set Body Type to Dynamic, use rb.velocity to set horizontal speed, and set velocity to zero when no input -> Option AQuick Check:
Dynamic + velocity control = smooth move and instant stop [OK]
- Using Static or Kinematic body types for physics movement
- Relying on AddForce without velocity control for instant stop
- Moving object by transform.position ignoring physics
