HIGH SCHOOL GAME DEVELOPMENT • UNITY 2D
Lesson 8: Add Rigidbody2D Physics
Difficulty: EasyTime: 45–60 minutesRigidbody2D • Gravity • PhysicsLesson 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
- Explain how physics affects objects in a 2D game.
- Add and configure a Rigidbody2D component.
- Experiment with different gravity settings.
- Use Collider2D components for collision detection.
- Write C# code for player movement.
- 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
- Unity Hub and Unity Editor
- A 2D Unity project (you may reuse Lesson 7)
- A square player sprite and a ground sprite
- A C# editor, such as Visual Studio or VS Code
4. Step-by-Step Instructions
Step 1 — Open the Unity Project
- Launch Unity Hub and open your existing 2D project, or create one named
Lesson8_Rigidbody2D. - Open the Scene view.
- Save the scene as
PhysicsScene.
Step 2 — Create the Player
- In the Hierarchy, right-click and select 2D Object → Sprites → Square (wording may vary by version).
- Rename the sprite
Player. - Set Position to (0, 2, 0) and Scale to (1, 1, 1).
- Choose a color for the player.
Step 3 — Add Rigidbody2D
- Select
Player. - In the Inspector, click Add Component and search for Rigidbody 2D.
- Configure the properties below.
| Property | Setting |
|---|---|
| Body Type | Dynamic |
| Gravity Scale | 1 |
| Linear Damping | 0 |
| Angular Damping | 0 |
| Constraints → Freeze Rotation Z | Checked |
Step 4 — Create the Ground
- Create another Square sprite and rename it
Ground. - Set Position to (0, -3, 0) and Scale to (10, 1, 1).
- Add a Box Collider 2D to Ground.
- Add a Box Collider 2D to Player if it does not already have one.
- Make sure Is Trigger is unchecked on both colliders.
- Leave Ground without a Rigidbody2D; it acts as a stationary surface.
Expected scene: The player falls toward the ground and stops at the collision surface.
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.
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
);
}
}
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
- Save the script and return to Unity.
- Wait for the C# compilation to finish.
- Set Move Speed to 5 and Gravity Scale to 1.
- Press Play and click in the Game view.
- Use A/D or the Left/Right arrow keys to move.
transform.position, because doing so can conflict with physics.5. Student Activity — Gravity Experiment
Make a prediction before each test. Run the scene with the specified setting and record your actual observation.
| Gravity Scale | Prediction | Actual Observation |
|---|---|---|
| 0 | ||
| 0.5 | ||
| 1 | ||
| 2 | ||
| 5 |
Answer the following:
- What happened at Gravity Scale 0?
- Which Gravity Scale made the player fall fastest?
- What happens if the Ground's Box Collider 2D is removed?
- Why does the player need both a Rigidbody2D and a Collider2D?
- How are
Update()andFixedUpdate()different?
6. Programming Challenge
Complete as many extensions as possible:
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.
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
- Completed Unity project or project folder, as directed by the teacher.
- Screenshot of the Player's Rigidbody2D Inspector settings.
- Screenshot showing the player on the ground during Play mode.
- Completed experiment table and written answers.
- File named
PlayerPhysicsMovement.cs.
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.