Course Content
Unity for Beginners
Unity for Beginners
OnCollisionEnter 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
-
OnCollisionEnter2D
: This is a special method in Unity that gets called automatically when the object this script is attached to collides with another object; -
Collision2D collision
: This part of the method gives you information about the collision, like which objects were involved; -
if (collision.collider.tag == "ground")
: This line checks if the object we collided with has a tag called "ground". Tags are like labels you can give to objects in Unity to identify them easily; -
Debug.Log("you lost");
: If the object we hit is tagged as "ground", this line will print "you lost" in the Unity Console. It's a way to give feedback or trigger events in your game.
-
OnTriggerEnter2D
: This is a special method in Unity that gets called automatically when the object this script is attached to enters a trigger zone of another object. Think of it like a sensor that detects when you walk through a doorway; -
Collider2D collision
: This part of the method gives you information about the object that entered the trigger zone. It's like a note that tells you which object you just walked through; -
if (collision.tag == "win")
: This line checks if the object we entered has a tag called "win". Tags are like labels you can give to objects in Unity to identify them easily. It's like checking if the object has a name tag that says "win"; -
Debug.Log("you won");
: If the object we entered is tagged as "win", this line will print "you won" in the Unity Console. It's a way to give feedback or trigger events in your game. Imagine it as a message that pops up to tell you that you've achieved something.
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?
Thanks for your feedback!