Unity Fundamentals: Movement, Physics, and Collisions
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.
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
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.
GameObject A | GameObject B | Result |
---|---|---|
Collider only | Collider only | No collision detected |
Collider + Rigidbody | Collider only | Collision detected |
Collider + Rigidbody | Collider + Rigidbody | Collision detected |
Best Practice
At least one GameObject in a collision must have a Rigidbody component for Unity to detect the collision.
Select the Car in the Hierarchy
Add a Rigidbody2D:
Configure the Rigidbody2D:
With the Car still selected
Add a Collider2D:
Create a Square sprite:
Position and scale:
Add a Box Collider 2D:
Change color (optional):
Create another Square sprite:
Position:
Add physics components:
Change color (optional):
Create a Circle sprite:
Position and scale:
Add and configure collider:
Change appearance:
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!");
}
}
}
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}");
}
Open the Console Window:
Play the game and test:
Collision vs Trigger Events
OnCollisionEnter2D / OnCollisionExit2D:
OnTriggerEnter2D / OnTriggerExit2D:
Open Package Manager:
Select Unity Registry:
Search for Cinemachine:
Install:
Add CinemachineCamera:
Create Virtual Camera:
Before submitting your project, ensure all features work correctly:
Time.deltaTime
for movementKeyboard.current
) for input handlingCongratulations 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!