2D Game Design Workshop - Part 2
Unity Fundamentals: Movement, Physics, and Collisions
In this workshop, you’ll practice the fundamentals of 2D game development in Unity. By the end of Part 1, you’ll have created a simple car game with movement controls, physics interactions, and a dynamic camera system.
This workshop is divided into three parts: - Part 1: Unity Fundamentals (this part) - Part 2: Enhancing Your Game with Physics and Collisions - Part 3: Polishing and Expanding Your Game
Each part builds upon the previous one, so be sure to complete them in order.
Workshop Overview
In this three-part series, you’ll learn the fundamentals of 2D game development in Unity. By the end of Part 2, you’ll have created a simple car game with movement controls, physics interactions, and a dynamic camera system.
Learning Objectives
By completing Part 2, you will: - Work with Rigidbody2D and Collider2D components - Handle collision and trigger events - Set up a Cinemachine camera to follow your player
Part 2 Starter Project
If you haven’t already, please complete Part 1 of the workshop to create the starter project for Part 2. Alternatively, you can clone the completed Part 1 project from here.
Step 1: Understanding Physics Components
Key Physics Concepts
Colliders
- Define the physical boundaries of GameObjects
- Determine what can be “hit” or “touched”
- Come in various shapes: Box, Circle, Polygon, Capsule
- Can be triggers (detect overlap) or solid (prevent overlap)
Rigidbodies
- Add physics simulation to GameObjects
- Enable gravity, forces, and realistic movement
- Required for physics-based collisions
- Control mass, drag, and other physical properties
Step 2: Collision Detection Rules
When Collisions Are Detected
GameObject A | GameObject B | Result |
---|---|---|
Collider only | Collider only | No collision detected |
Collider + Rigidbody | Collider only | Collision detected |
Collider + Rigidbody | Collider + Rigidbody | Collision detected |
At least one GameObject in a collision must have a Rigidbody component for Unity to detect the collision.
Step 3: Add Physics to Your Car
Adding Components to Your Car
Select the Car in the Hierarchy
Add a Rigidbody2D:
- Click “Add Component” in the Inspector
- Search for “Rigidbody2D”
- Click to add it
Configure the Rigidbody2D:
- Set Gravity Scale to 0 (we don’t want the car falling)
- Set Linear Damping to 1 (adds some resistance)
- Set Angular Damping to 1 (slows rotation)
Step 4: Add a Collider to Your Car
Completing the Physics Setup
With the Car still selected
Add a Collider2D:
- Click “Add Component”
- Choose “Capsule Collider 2D”
- It should automatically fit your sprite
Step 5: Create a Static Wall
Static Obstacle (Wall)
Create a Square sprite:
- Right-click in Hierarchy → 2D Object → Sprites → Square
- Rename to “Wall”
Position and scale:
- Position: (5, 0, 0)
- Scale: (1, 3, 1) to make it tall
Add a Box Collider 2D:
- Add Component → Box Collider 2D
- No Rigidbody needed (static object)
Change color (optional):
- Set Sprite Renderer color to gray5
Step 6: Create a Dynamic Box
Dynamic Obstacle (Box)
Create another Square sprite:
- Right-click in Hierarchy → 2D Object → Sprites → Square
- Rename to “Box”
Position:
- Position: (-3, 2, 0)
Add physics components:
- Add Component → Box Collider 2D
- Add Component → Rigidbody2D
- Set Gravity Scale to 0
Change color (optional):
- Set Sprite Renderer color to brown
Step 7: Create a Trigger Zone
Trigger Zone (Checkpoint)
Create a Circle sprite:
- Right-click in Hierarchy → 2D Object → Sprites → Circle
- Rename to “Checkpoint”
Position and scale:
- Position: (0, 5, 0)
- Scale: (2, 2, 1)
Add and configure collider:
- Add Component → Circle Collider 2D
- Check “Is Trigger” checkbox
Change appearance:
- Set Sprite Renderer color to green
- Set Color alpha to 0.5 for transparency
Step 8: Add Collision Detection Code
Implementing Collision Methods
Create a script called Collision to include collision detection:
using UnityEngine;
using UnityEngine.InputSystem;
public class Collision : MonoBehaviour
{
[SerializeField] float moveSpeed = 5f;
[SerializeField] float steerSpeed = 100;
// Called when this collider/rigidbody hits another collider/rigidbody
void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log($"Car crashed into: {collision.gameObject.name}");
// Different reactions based on what we hit
if (collision.gameObject.name == "Wall")
{
Debug.Log("Ouch! Hit a wall!");
}
else if (collision.gameObject.name == "Box")
{
Debug.Log("Pushed a box!");
}
}
}
Step 9: Add Trigger Detection Code
Complete Collision and Trigger Detection
Add these methods to your Cruise script (after the Update method):
// Called when collision ends
void OnCollisionExit2D(Collision2D collision)
{
Debug.Log($"Car separated from: {collision.gameObject.name}");
}
// Called when entering a trigger zone
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log($"Car entered trigger: {other.gameObject.name}");
if (other.gameObject.name == "Checkpoint")
{
Debug.Log("Checkpoint reached!");
// You could add score, play sound, etc.
}
}
// Called when exiting a trigger zone
void OnTriggerExit2D(Collider2D other)
{
Debug.Log($"Car exited trigger: {other.gameObject.name}");
}
Step 10: Test Collision Detection
Testing Your Physics
Open the Console Window:
- Window → General → Console
- Dock it somewhere visible
Play the game and test:
- Cruise into walls - see “crashed into” messages
- Push boxes around - see collision messages
- Cruise through checkpoints - see trigger messages
OnCollisionEnter2D / OnCollisionExit2D:
- Physical collisions where objects bounce/push
- Both objects need colliders, at least one needs Rigidbody2D
- Objects cannot pass through each other
OnTriggerEnter2D / OnTriggerExit2D:
- Detection zones where objects can pass through
- Collider must have “Is Trigger” checked
- Used for checkpoints, power-ups, detection areas
Step 11: Install Cinemachine
Adding the Cinemachine Package
Open Package Manager:
- Window → Package Manager
Select Unity Registry:
- Dropdown at top-left → Unity Registry
Search for Cinemachine:
- Type “Cinemachine” in search bar
Install:
- Click on Cinemachine package
- Click “Install” button
- Wait for installation to complete and then close the package manager
Step 12: Setup Cinemachine Brain
Configure Main Camera
Add CinemachineCamera:
- Right-click the Scene in the Hieararchy and select Cinemachine → Cinemachine Camera
- Set the Tracking Target to the Car GameObject
- Change the Orthographic Size to 6 (zoom level)
- Change the Positional Control to “Positional Composer”
- Leave default settings
Step 13: Create Virtual Camera
Adding the Follow Camera
Create Virtual Camera:
- Right-click in Hierarchy
- Cinemachine → Targeted Camera → Follow Camera
- Rename to “CarFollowCamera”
Step 14: Test the Camera
Verify Camera Following
- Press Play
- Cruise your car around using WASD
- Notice how the camera smoothly follows your car
- Try adjusting Damping values (1-3) to see different follow speeds
Testing Checklist
Before submitting your project, ensure all features work correctly:
Movement
Physics
Triggers
Camera
Troubleshooting Guide
Common Issues and Solutions
Car doesn’t move / Keyboard not responding
- Check if the Input System is enabled (Edit → Project Settings → Player → Active Input Handling)
- Ensure Cruise script is attached to Car
- Verify speed values aren’t 0
- Ensure Time Scale is 1 (Edit → Project Settings → Time)
- Make sure Game view has focus (click inside it)
NullReferenceException with Keyboard.current
- The Input System Package may not be installed
- Go to Window → Package Manager
- Search for “Input System” and install it
- Restart Unity when prompted
Collisions not detected
- Ensure at least one object has Rigidbody2D
- Check that both objects have Collider2D components
- Verify colliders aren’t set as triggers when they shouldn’t be
Car falls or flies away
- Set Rigidbody2D Gravity Scale to 0
- Check Body Type is “Dynamic”
- Freeze Z rotation if car spins uncontrollably
Camera doesn’t follow
- Verify Car is assigned to Virtual Camera’s Follow field
- Check Cinemachine Brain is on Main Camera
Resources and References
Unity Documentation
Keyboard Shortcuts
- Play/Stop: Ctrl/Cmd + P
- Pause: Ctrl/Cmd + Shift + P
- Save Scene: Ctrl/Cmd + S
- Duplicate: Ctrl/Cmd + D
- Delete: Delete key
- Undo: Ctrl/Cmd + Z
- Focus GameObject: F key (with object selected)
Best Practices Learned
- Always use
Time.deltaTime
for movement - Use modern Input System (
Keyboard.current
) for input handling - Organize your Hierarchy with empty GameObjects
- Test frequently during development
- Use Debug.Log for troubleshooting
- Save your work often
Conclusion
Congratulations on completing Part 2 of the 2D Game Design Workshop! You’ve learned fundamental Unity concepts including transforms, physics, collisions, and camera systems. These skills form the foundation for any 2D game development project.
In Part 3, you’ll enhance your game further by adding art assets, sound effects, and polishing gameplay mechanics. Keep experimenting and building on what you’ve learned!