LibraryCollision Detection and Triggers

Collision Detection and Triggers

Learn about Collision Detection and Triggers as part of Game Development with Unity and C#

Game Development: Physics and Collision Detection in Unity

In game development, understanding how objects interact is crucial for creating immersive and responsive experiences. Collision detection is the process by which a game engine determines if two or more objects are overlapping or intersecting. This forms the backbone of many gameplay mechanics, from characters interacting with the environment to projectiles hitting targets.

Core Concepts: Colliders and Rigidbodies

Unity uses two primary components to manage physics and collisions: <b>Colliders</b> and <b>Rigidbodies</b>. Colliders define the physical shape of an object for the purpose of collision detection, while Rigidbodies enable objects to be affected by physics, such as gravity, forces, and collisions.

ComponentPurposeKey Features
ColliderDefines the physical shape for collision detection.Can be primitive (Box, Sphere, Capsule) or mesh-based. Does not inherently move or react to physics on its own.
RigidbodyEnables an object to be controlled by the physics engine.Responds to gravity, forces, and collisions. Requires a Collider to interact with other physics objects.

Types of Colliders

Unity offers various Collider types to represent different shapes. Choosing the right Collider is important for performance and accuracy. Primitive colliders are generally more performant than Mesh Colliders.

Primitive Colliders are efficient for simple shapes.

Box, Sphere, and Capsule colliders are the most common and performant options for representing basic geometric shapes. They are ideal for characters, projectiles, and simple environmental objects.

<b>Box Collider:</b> Represents a rectangular prism. Useful for cubes, walls, and rectangular platforms. <b>Sphere Collider:</b> Represents a sphere. Ideal for balls, grenades, or circular objects. <b>Capsule Collider:</b> Represents a capsule shape (a cylinder with hemispherical ends). Excellent for characters, as it can smoothly move through tight spaces and represent a character's general volume.

Mesh Colliders can represent complex shapes but are more resource-intensive.

Mesh Colliders use the object's actual mesh to define its collision shape. This allows for highly accurate collisions with complex geometry but can be computationally expensive, especially for dynamic objects.

<b>Mesh Collider:</b> Uses the object's mesh asset for collision. Can be set to 'Convex' for dynamic collisions (required for Rigidbodies) or 'Non-Convex' for static environments. Use 'Non-Convex' Mesh Colliders only for static geometry to optimize performance.

Collision Detection Events

Unity's physics engine provides several C# scripting functions that are called automatically when collisions occur. These functions allow you to react to collisions and implement game logic.

When two objects with Rigidbodies collide, the OnCollisionEnter, OnCollisionStay, and OnCollisionExit functions are called on the scripts attached to those objects. These functions receive a Collision object, which contains information about the collision, such as the contact points and the other object involved.

📚

Text-based content

Library pages focus on text content

Loading diagram...

Triggers vs. Collisions

Sometimes, you don't want a physical reaction to an overlap, but rather to detect that an object has entered a specific area. This is where <b>Triggers</b> come in. By checking the 'Is Trigger' box on a Collider component, the collider will no longer cause physical collisions but will instead send trigger events.

FeatureCollisionTrigger
Physical InteractionYes, objects bounce off each other.No, objects pass through each other.
Scripting EventsOnCollisionEnter, OnCollisionStay, OnCollisionExitOnTriggerEnter, OnTriggerStay, OnTriggerExit
Rigidbody RequirementAt least one object must have a Rigidbody.At least one object must have a Rigidbody.
Use CasePhysical interactions, blocking movement, bouncing.Detecting entry into an area, activating events, collecting items.

When a Collider marked as a Trigger overlaps with another Collider (at least one of which must have a Rigidbody), the

code
OnTriggerEnter
,
code
OnTriggerStay
, and
code
OnTriggerExit
functions are called on the scripts attached to those objects. These functions receive a
code
Collider
object, which refers to the other collider involved in the trigger event.

Remember: For any collision or trigger event to be detected, at least one of the interacting GameObjects must have a Rigidbody component attached.

Practical Application: Collecting Items

A common use case for triggers is collecting items. Imagine a player character walking over a coin. You'd want the coin to disappear and the player's score to increase, but you wouldn't want the player to physically collide with the coin. This is a perfect scenario for a trigger.

What is the primary difference between a Collider and a Trigger in Unity?

A Collider defines a physical shape for collision detection and interaction, while a Trigger defines a region that detects overlaps without causing physical reactions.

Which Unity script functions are called when two objects with Rigidbodies collide?

OnCollisionEnter, OnCollisionStay, and OnCollisionExit.

Which Unity script functions are called when a Trigger overlaps with another Collider (with a Rigidbody)?

OnTriggerEnter, OnTriggerStay, and OnTriggerExit.

Learning Resources

Unity Manual: Physics(documentation)

The official Unity documentation provides a comprehensive overview of the physics system, including colliders, rigidbodies, and forces.

Unity Manual: Colliders(documentation)

Detailed explanation of different collider types (Box, Sphere, Capsule, Mesh) and their properties in Unity.

Unity Manual: Rigidbodies(documentation)

Learn about the Rigidbody component, its role in physics simulation, and its various parameters.

Unity Learn: Physics and Collision(tutorial)

A learning pathway from Unity Learn covering fundamental physics concepts and collision detection in Unity.

Unity Scripting API: Collider(documentation)

Reference for the Collider class and its methods, including trigger-related functions.

Unity Scripting API: Collision(documentation)

API documentation for the Collision class, detailing the information available during a collision event.

Unity Scripting API: OnTriggerEnter(documentation)

Official Unity documentation for the OnTriggerEnter callback function.

Unity Scripting API: OnCollisionEnter(documentation)

Official Unity documentation for the OnCollisionEnter callback function.

Brackeys: Unity Physics Tutorial(video)

A popular YouTube tutorial by Brackeys explaining Unity's physics system and collision detection with practical examples.

GameDev.tv: Unity Physics and Collision(video)

A video tutorial from GameDev.tv covering the basics of physics and collision detection in Unity, including triggers.