Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
OnCollisionEnter and OnTriggerEnter | Unity Physics
course content

Contenido del Curso

Unity for Beginners

OnCollisionEnter and OnTriggerEnterOnCollisionEnter and OnTriggerEnter

OnCollisionEnter2D:

Usage: This method is called when a 2D collider attached to the GameObject this script is attached to, collides with another collider in the scene and at least one of the colliders has a Rigidbody2D attached.

Trigger: This method is mainly used for handling physical collisions, where objects actually "bump" into each other.

Example Scenario: Imagine you have a game with a character that jumps and lands on platforms. When the character lands on a platform, you may want to play a landing sound effect, change the character's animation, or trigger some other action. You would use OnCollisionEnter2D to detect when the character's collider collides with the platform's collider.

OnTriggerEnter2D:

Usage: This method is called when a 2D collider attached to the GameObject this script is attached to, overlaps with another collider in the scene and at least one of the colliders has the "Is Trigger" property enabled.

Trigger: Unlike OnCollisionEnter2D, this method is not about physical collisions, but rather about detecting when one object enters the space of another without necessarily stopping their motion. Think of it like walking through a door without physically pushing it open.

Example Scenario: Let's say you have a game where the player collects coins. When the player's character moves over a coin, you want to increase the player's score and make the coin disappear. You would use OnTriggerEnter2D to detect when the player's collider overlaps with the coin's collider.

What is a Tag?

Definition: A tag is a string value that you can assign to a GameObject in Unity.

Purpose: Tags are used to categorize and identify GameObjects for various purposes, such as differentiating between player-controlled objects, enemies, collectibles, obstacles, etc.

Common Usages of Tags:

Identifying Game Objects: Tags are commonly used to identify important GameObjects in your scene, such as the player character, enemies, collectibles, goals, etc.

Collision Detection: Tags can be used to determine the type of GameObjects involved in collisions. For example, you might want to check if the player collided with an enemy or a collectible.

Trigger Detection: Similar to collision detection, you can use tags to identify GameObjects that enter trigger zones set up in your scene.

Example:

This method is called whenever a collision occurs between the player and another GameObject in the scene.

The method receives a parameter collision of type Collision2D, which contains information about the collision.

It checks if the tag of the collider involved in the collision is equal to "ground".

If the condition is true, it prints "you lost" to the Unity Console using Debug.Log().

This method is called whenever a trigger overlap occurs between the player and another GameObject in the scene.

The method receives a parameter collision of type Collider2D, which contains information about the trigger overlap.

It checks if the tag of the triggering collider is equal to "win".

If the condition is true, it prints "you won" to the Unity Console using Debug.Log().

1. When using OnTriggerEnter2D, which property of colliders must be set to true to enable trigger detection?
2. Which method should be used to handle collision detection between two colliders that are not triggers?

When using OnTriggerEnter2D, which property of colliders must be set to true to enable trigger detection?

Selecciona la respuesta correcta

Which method should be used to handle collision detection between two colliders that are not triggers?

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 3. Capítulo 4
course content

Contenido del Curso

Unity for Beginners

OnCollisionEnter and OnTriggerEnterOnCollisionEnter and OnTriggerEnter

OnCollisionEnter2D:

Usage: This method is called when a 2D collider attached to the GameObject this script is attached to, collides with another collider in the scene and at least one of the colliders has a Rigidbody2D attached.

Trigger: This method is mainly used for handling physical collisions, where objects actually "bump" into each other.

Example Scenario: Imagine you have a game with a character that jumps and lands on platforms. When the character lands on a platform, you may want to play a landing sound effect, change the character's animation, or trigger some other action. You would use OnCollisionEnter2D to detect when the character's collider collides with the platform's collider.

OnTriggerEnter2D:

Usage: This method is called when a 2D collider attached to the GameObject this script is attached to, overlaps with another collider in the scene and at least one of the colliders has the "Is Trigger" property enabled.

Trigger: Unlike OnCollisionEnter2D, this method is not about physical collisions, but rather about detecting when one object enters the space of another without necessarily stopping their motion. Think of it like walking through a door without physically pushing it open.

Example Scenario: Let's say you have a game where the player collects coins. When the player's character moves over a coin, you want to increase the player's score and make the coin disappear. You would use OnTriggerEnter2D to detect when the player's collider overlaps with the coin's collider.

What is a Tag?

Definition: A tag is a string value that you can assign to a GameObject in Unity.

Purpose: Tags are used to categorize and identify GameObjects for various purposes, such as differentiating between player-controlled objects, enemies, collectibles, obstacles, etc.

Common Usages of Tags:

Identifying Game Objects: Tags are commonly used to identify important GameObjects in your scene, such as the player character, enemies, collectibles, goals, etc.

Collision Detection: Tags can be used to determine the type of GameObjects involved in collisions. For example, you might want to check if the player collided with an enemy or a collectible.

Trigger Detection: Similar to collision detection, you can use tags to identify GameObjects that enter trigger zones set up in your scene.

Example:

This method is called whenever a collision occurs between the player and another GameObject in the scene.

The method receives a parameter collision of type Collision2D, which contains information about the collision.

It checks if the tag of the collider involved in the collision is equal to "ground".

If the condition is true, it prints "you lost" to the Unity Console using Debug.Log().

This method is called whenever a trigger overlap occurs between the player and another GameObject in the scene.

The method receives a parameter collision of type Collider2D, which contains information about the trigger overlap.

It checks if the tag of the triggering collider is equal to "win".

If the condition is true, it prints "you won" to the Unity Console using Debug.Log().

1. When using OnTriggerEnter2D, which property of colliders must be set to true to enable trigger detection?
2. Which method should be used to handle collision detection between two colliders that are not triggers?

When using OnTriggerEnter2D, which property of colliders must be set to true to enable trigger detection?

Selecciona la respuesta correcta

Which method should be used to handle collision detection between two colliders that are not triggers?

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 3. Capítulo 4
some-alt