HIGH SCHOOL GAME DEVELOPMENT • UNITY 2D

Lesson 8: Add Rigidbody2D Physics

Difficulty: EasyTime: 45–60 minutesRigidbody2D • Gravity • Physics

Lesson Overview

In this lesson, you will add physics to a 2D player character in Unity. You will attach a Rigidbody2D, experiment with gravity, create collision surfaces, and write a C# script to move the player. By the end, your player will fall, land on the ground, and move horizontally with the keyboard.

1. Learning Objectives

  1. Explain how physics affects objects in a 2D game.
  2. Add and configure a Rigidbody2D component.
  3. Experiment with different gravity settings.
  4. Use Collider2D components for collision detection.
  5. Write C# code for player movement.
  6. Test and troubleshoot common physics problems.

2. Key Vocabulary

Term Meaning
Rigidbody2D A component that lets a 2D object participate in Unity physics.
Gravity Acceleration that pulls objects downward.
Collider2D A shape Unity uses to detect physical contact.
Dynamic A Rigidbody2D body type that responds to gravity and collisions.
Gravity Scale A multiplier controlling how strongly gravity accelerates an object.
Linear Velocity The rate and direction of an object's movement.
FixedUpdate() A method used for physics-related updates at fixed intervals.

3. Materials

4. Step-by-Step Instructions

Step 1 — Open the Unity Project

  1. Launch Unity Hub and open your existing 2D project, or create one named PX_Rigid_Body_L08_lastname.
  2. Open the Scene view.
  3. Save the scene as PhysicsScene.

Step 2 — Create the Player

  1. In the Hierarchy, right-click and select 2D Object → Sprites → Square (wording may vary by version).
  2. Rename the sprite Player.
  3. Set Position to (0, 2, 0) and Scale to (1, 1, 1).
  4. Choose a color for the player.

Step 3 — Add Rigidbody2D

  1. Select Player.
  2. In the Inspector, click Add Component and search for Rigidbody 2D.
  3. Configure the properties below.
Property Setting
Body Type Dynamic
Gravity Scale 1
Linear Damping 0
Angular Damping 0
Constraints → Freeze Rotation Z Checked
Try it: Press Play. What happens to the player? The player should fall due to gravity.

Step 4 — Create the Ground

  1. Create another Square sprite and rename it Ground.
  2. Set Position to (0, -3, 0) and Scale to (10, 1, 1).
  3. Add a Box Collider 2D to Ground.
  4. Add a Box Collider 2D to Player if it does not already have one.
  5. Make sure Is Trigger is unchecked on both colliders.
  6. Leave Ground without a Rigidbody2D; it acts as a stationary surface.
Player Gravity Ground

Expected scene: The player falls toward the ground and stops at the collision surface.

Check: Press Play again. The player should land on the ground rather than fall forever.

Step 5 — Test Gravity

Select Player and change Rigidbody2D → Gravity Scale. Test values of 0, 0.5, 1, 2, and 5. Observe how quickly the player accelerates downward.

1.0
Ground

Gravity is active: the player accelerates downward.

This is a conceptual illustration, not a real-time physics simulation.

Step 6 — Program Player Movement

Create a script named PlayerPhysicsMovement.cs, paste in the following code, and attach it to Player:

using UnityEngine;

public class PlayerPhysicsMovement : MonoBehaviour
{
    public float moveSpeed = 5f;

    private Rigidbody2D rb;
    private float horizontalInput;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        // Read left/right keyboard input.
        horizontalInput = Input.GetAxisRaw("Horizontal");
    }

    void FixedUpdate()
    {
        // Preserve vertical velocity so gravity still works.
        rb.linearVelocity = new Vector2(
            horizontalInput * moveSpeed,
            rb.linearVelocity.y
        );
    }
}
Unity version notes: The code uses Unity 6's Rigidbody2D.linearVelocity. On Unity 2022 or 2023, replace linearVelocity with velocity. This example uses the legacy Input Manager; set Edit → Project Settings → Player → Active Input Handling to Both or Input Manager (Old), if needed.

Step 7 — Test the Player

  1. Save the script and return to Unity.
  2. Wait for the C# compilation to finish.
  3. Set Move Speed to 5 and Gravity Scale to 1.
  4. Press Play and click in the Game view.
  5. Use A/D or the Left/Right arrow keys to move.
Expected   0.5     1     2     5    

Answer the following:

  1. What happened at Gravity Scale 0?
  2. Which Gravity Scale made the player fall fastest?
  3. What happens if the Ground's Box Collider 2D is removed?
  4. Why does the player need both a Rigidbody2D and a Collider2D?
  5. How are Update() and FixedUpdate() different?

6. Programming Challenge

Complete as many extensions as possible:

Bonus: Add a Space-key jump using an upward Rigidbody2D velocity or impulse. Include a grounded check to prevent unlimited midair jumping.

7. Troubleshooting

Problem Possible Fix
Player falls through ground Ensure both objects have Collider2D components and Is Trigger is off.
Player does not fall Set Body Type to Dynamic and Gravity Scale above 0.
Player tips over Enable Freeze Rotation Z.
Player will not move Check script attachment, Game view focus, input settings, and Console errors.
linearVelocity error Use velocity in older Unity versions.

8. Knowledge Check

Choose the best answer for each question, then select Check Answers.

1. Which component gives a 2D player physics behavior?

Correct: Rigidbody2D enables physics behavior.

2. What happens when Gravity Scale is 0?

Correct: A scale of 0 removes gravity acceleration on this body.

3. Which component helps an object collide with the ground?

Correct: Box Collider 2D defines a collision shape.

4. Which method is commonly used for physics updates?

Correct: FixedUpdate runs at fixed physics intervals.

5. What does Freeze Rotation Z do?

Correct: Locking Z rotation prevents tipping/spinning.

9. Grading Rubric

Criteria Points
Player GameObject created correctly 15
Rigidbody2D configured correctly 20
Ground and colliders work 20
Player movement script works 20
Gravity experiment completed 15
Written questions answered 10
Total 100

10. Submission Requirements

  1. Completed Unity project or project folder, as directed by the teacher.
  2. Screenshot of the Player's Rigidbody2D Inspector settings.
  3. Screenshot showing the player on the ground during Play mode.
  4. Completed experiment table and written answers.
  5. File named PlayerPhysicsMovement.cs.
Success Criteria: The player falls naturally, stops at the ground, and moves left and right with the keyboard.

11. Lesson Summary

Rigidbody2D allows Unity physics to control a 2D object. Gravity Scale changes downward acceleration, colliders detect contact and keep objects from passing through each other, and Rigidbody2D velocity controls movement.

Next Lesson — Lesson 9: Make the Player Jump. Add a jump mechanic and detect when the player is standing on the ground.