0
0
Unityframework~10 mins

Rigidbody2D component in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Rigidbody2D component
Add Rigidbody2D to GameObject
Physics Engine Controls Movement
Apply Forces or Velocity
Update Position & Rotation
Collision Detection & Response
Repeat Each Frame
The Rigidbody2D component lets Unity's physics engine move and rotate a 2D object by applying forces and detecting collisions every frame.
Execution Sample
Unity
Rigidbody2D rb = GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(5, 0);
// Moves object right at speed 5

void FixedUpdate() {
  rb.AddForce(new Vector2(0, 10));
}
This code sets a 2D object's velocity to move right and applies an upward force every physics update.
Execution Table
StepActionVelocityPosition ChangeNotes
1Set velocity to (5,0)(5,0)Object starts moving rightInitial velocity set
2Physics update applies velocity(5,0)Position moves right by velocity * deltaTimeObject moves right
3Add upward force (0,10)(5, small positive Y)Velocity Y increases slightlyForce changes velocity
4Physics update applies new velocity(5, increased Y)Position moves right and upObject moves diagonally
5Repeat physics updatesVelocity changes by forces and dragPosition updates accordinglyContinuous movement and collision handling
6Collision detectedVelocity may changePosition adjusted by collision responseObject reacts to collisions
7No more forces appliedVelocity decreases due to dragMovement slowsObject eventually stops if no forces
8End of traceFinal velocity and positionFinal position after updatesSimulation continues each frame
💡 Simulation runs continuously; this trace shows initial velocity and force effects over a few physics updates.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4Final
rb.velocity(0,0)(5,0)(5, small positive Y)(5, increased Y)Varies with forces and drag
Position(x0,y0)(x0 + deltaX, y0)(x0 + deltaX, y0 + deltaY)(x0 + deltaX2, y0 + deltaY2)Updates each frame
Key Moments - 3 Insights
Why does the velocity change after applying AddForce even though we set velocity directly before?
Setting velocity directly (Step 1) sets initial speed. AddForce (Step 3) adds acceleration, changing velocity over time as physics updates (see Steps 3 and 4 in execution_table).
Does Rigidbody2D move the object instantly when velocity changes?
No, Rigidbody2D updates position during physics steps (Step 2 and 4). Velocity affects how much position changes each frame, not instant teleport.
What happens when a collision occurs to velocity and position?
Collision changes velocity and adjusts position to prevent overlap (Step 6). Rigidbody2D handles this automatically during physics updates.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3. What happens to the velocity after AddForce is applied?
AVelocity's Y component increases slightly
BVelocity resets to zero
CVelocity's X component becomes zero
DVelocity does not change
💡 Hint
Check the 'Velocity' column at Step 3 in execution_table.
At which step does the object start moving diagonally?
AStep 1
BStep 2
CStep 4
DStep 6
💡 Hint
Look for when velocity has both X and Y components in execution_table.
If no forces are applied after Step 6, what happens to velocity over time?
AVelocity increases
BVelocity decreases due to drag
CVelocity stays constant
DVelocity instantly becomes zero
💡 Hint
See the 'Velocity' and 'Notes' columns at Step 7 in execution_table.
Concept Snapshot
Rigidbody2D lets Unity move 2D objects using physics.
Set velocity to control speed directly.
Use AddForce to apply acceleration.
Physics updates position each frame.
Collisions change velocity and position automatically.
Drag slows velocity if no forces act.
Full Transcript
The Rigidbody2D component in Unity controls 2D object movement using physics. When you add Rigidbody2D to a game object, Unity's physics engine updates its position and rotation every frame. You can set the velocity directly to move the object at a constant speed, or apply forces to accelerate it. Each physics update applies velocity to change position. Collisions are detected and handled automatically, adjusting velocity and position to prevent overlaps. Velocity changes gradually with forces and drag, so the object moves smoothly. This trace shows setting velocity to move right, then adding an upward force to move diagonally, and how collisions and drag affect movement over time.