Collision Detection

Big Picture:

  • Each GameObject that is colliding needs some type of "Collider"
  • At least one of the GameObject's needs to have a "Rigidbody", because this is the class that has collision detection
  • Each collider can be either a:
    • collider - objects bump into it and change speed/momentum.
    • trigger - objects go through it as though it were not there, but register that it was triggered.
  • We use if statements to check the tag of the object we hit, and respond accordingly

Useful Vocabulary

  • gameObject - A way to refer to "this", the GameObject that is running the script. Capital "GameObject" is the class, lowercash "gameObject" means "this" one specific one
  • tag - Each GameObject can have a tag, that is used for quickly checking the type of object you collided with. There may be 100 types of enemies, but all are tagged as "enemy".
  • collider - this is like the physical body of the GameObject. This is the region that will bump into other objects, regardless of what the sprite renderer looks like.
  • trigger - if you check is "Is Trigger" box on any collider component, it now acts like the object was not physically there. Other objects can pass right through it, but it will still detect collisions. This is a great way to detect things like a player reaching a checkpoint.


Useful Commands

  • void OnCollisionEnter2D (Collision2D other) { } - This is the method that gets called whenever the object with this script collides with another object (not a trigger, that is a separate method). Both GameObjects involved in the collision have this method called on all of their scripts. The collision generates an object of type "Collision2D" that holds a ton of useful information such as: (1) The object you hit (2) The point where you hit ... and much more.
  • void OnTriggerEnter2D (Collider2D other) { } - Exactly the same as the last one except this one gets called when one or both of the objects have triggers instead of colliders. As such, there was no actual collision. The objects just kept going in their same direction. So instead of a Collision2D object being generated, you just get a reference to the Collider2D that hit the trigger.
  • if (other.gameObject.CompareTag("enemy") { } - Inside either of the previous commands, once the collision/trigger method has been called, you want to use an if statement to check the tag of the object and see what you hit. For example, if it is a coin then play a coin sound and add one to the score, but if you hit an enemy then die.
  • Debug.Log() - Often the collision detection doesn't immediately work the way you want. It can be really useful to use Debug.Log() to print info to the Console Window to see what is happening, such as: Debug.Log("I hit something"); ... or ... Debug.Log("I hit: " + other.gameObject);

How to Change the Tag of a GameObject


Step 1 - Click on a GameObject. At the top of the Inspector Window is the drop-down menu for "Tag". Click on the drop-down menu and select "Add Tag ..."


2. Then click the plus sign to create a new Tag


3. Make sure every tag you want to use has been created. These are case and spelling sensitive so keep to one theme (e.g. every tag is capitalized or every tag is all lowercase letters)


4. TAG YOUR GAMEOBJECT. By far this is the most often forgotten step. Steps 1-3 just created a new tag, but did not assign it to any GameObject. You need to click on the GameObject you want to tag, and select the new tag from the drop-down menu.

Useful Code Snippets


Collision Example 1

Simple example of two objects colliding and printing a message to the Console Window (neither has "is Trigger" checked).


Collision Example 2

Example of colliding with an object, checking it's tag and doing two different things depending on the tag.


Trigger Example

Example of hitting a trigger, and using an if statement to respond accordingly.

Other Great Resources

Video...

  1. "Unity - How to Detect Collision in C# [Using Colliders]" by Press Start - A very solid 8min video from Nov 2018 that goes over the basics of colliders, triggers and how to use them in C# code.


Unity Official Tutorials and Videos

  1. "Beginner 2D UFO Game 4 of 9: Adding Collision - Unity Official Tutorials" by Unity - A 9min video from Jan 2016 that goes over all of the basics of detecting collisions and how to use them.
  2. "Collider 2D - Official Unity Tutorial" by Unity - A slower 6min video from April 2014 that goes in depth into how all of the 2D Colliders work, what they really do, and how to use them.