Unity Collision 2D and 3D: Super Simple Guide

Collision detection is required for all types of games. Detecting and utilizing the collision is implemented in a different manner in different game engines. Unity Collision works based on the collider component that can be added to any Gameobject. Also, you need to understand how colliders and Unity Rigidbody work together to efficiently use them. Without a basic understanding of how Unity Colliders work, it will be very difficult to code the game mechanics. In this post, we will see how to use colliders in Unity and where to use OnCollisionEnter and OnTriggerEnter functions.

Introduction to Unity Colliders

Unity colliders are very simple to use. Unity has divided the colliders into 2D and 3D. So, you have to be very careful which collider you are using. All your codes and collision detection will change to 2D and 3D. Further, there are types of colliders, static collider, Rigidbody collider, kinematic Rigidbody collider, etc. The collider used in all these are the same, the type is for how the object to which the collider is added behaves when collided with.

Unity Collider Types

Static collider

Static colliders are considered to be non-moving objects by Unity. Do not confuse static Gameobject with the static collider. If a Rigidbody is not attached to a collider then it’s a static collider. They do not move when an object Collides to with them. Though, they can be moved using transform, moving a static collider leads to performance loss during runtime.

Rigidbody collider or Dynamic Collider

A Rigidbody collider works like a real-world object. It is governed by the forces of physics. If you apply force on it, it will move. In Unity, Rigidbody is used on an object which will move a lot during gameplay. Check the screenshot for how a Rigidbody is attached to a game object.

Kinematic Rigidbody collider

A kinematic Rigidbody is a Rigidbody that behaves like a static object. So, the next question is then why use a kinematic Rigidbody. The main reason to use a kinematic Rigidbody is to tell Unity that the object does not respond to forces but will be movable by script during runtime. The performance loss is very less in moving a kinematic Rigidbody compared to a Static object.

Unity Collision Detection 3D

Adding a 3D collider in Unity

To add a collider to your Gameobject, select the object in Hierarchy view and click on Add Component in the Inspector window. Then select the type of Collider based on your requirement.

You can adjust the size of the collider using the Edit Collider button.

Unity Collider in inspector

Non-trigger and Trigger collider

In Unity you can mark a collider as trigger using the check box in the inspector window. The behavior of the object changes if it is marked as a trigger.

Unity Colliders not marked as trigger

Unity will detect a collision only if one of the objects is a Rigidbody. That is, if you have two Gameobject marked as static colliders then collision between them will not be detected. It’s the same case with two kinematic Rigidbody. If you use the OnCollisionEnter function in any of the above cases, the function will not be called during a Collision. Find the collision matrix for unity colliders in the image below.

collision matrix for Non trigger unity colliders

Ref: Unity docs

Unity collider marked as Trigger

When you mark a collider as a trigger, it no longer behaves like a solid object. It allows the other object to pass through it. But the time of one object entering another object’s space can trigger a function. With that, you can know if a collision has happened without actually creating a collision. You have to use the OnTriggerEnter function if the collider is Marked as a trigger. The collision matrix is a little different in the case of triggers.

Trigger collision matrix

Trigger collision doesn’t work in Unity if both the colliders are static colliders. In all the other cases the OnTriggerEnter is called. Knowing these basic things about colliders will help you set them up easily in your game.

If you did not understand the matrix completely then here is a simple comparison to help you. Consider static colliders as objects that don’t move like walls. Rigidbody collider denotes a moving object like a ball. Kinematic Rigidbody denotes that the object is not moved by physics but can be moved by script or animation.

Unity does not want to detect collision between two static objects so normal collision is detected only if one of the Gameobject is movable by physics. Whereas trigger function works a little differently. It works only if one of the colliders is marked as trigger and can be moved either by physics or script.

Setting Collision Layers in Unity

One you have added your colliders and Rigidbody now it’s time to set the layers. Unity gives you control to decide which layers collide with each other.

This setting is available in Edit>Project settings> Physics

Layer collision setting in Unity

You can just uncheck the layers that you don’t want to collide. For example, if you don’t want the friend layer to collide with the water layer. Then just uncheck the box in water row of the Friend colomn.

Remember to create all your layers first before editing this setting.

3D collider shapes in Unity

Collider shapeWhere to useEffect on Performance
Box ColliderObject shaped like cubeLess Performance intensive
Sphere Collider3D round shaped objectsLess Performance intensive
Capsule CollidersFor CharactersLess Performance intensive
Mesh CollidersObjects with Uneven shapesPerformance intensive
Terrain CollidersFor Unity terrainsVery Performance intensive

Detect Collision in Unity 3D

  1. Create a new script. The new script contains Start and Update functions.
  2. Add the script to your player Gameobject.
  3. Add Rigidbody component to Player.
  4. Make sure IsKinematic is Unchecked.
  5. Add collider to player and enemy Gameobjects.
  6. Make sure your layers are set correctly and the layer matrix settings are right.
  7. Add the function below to the new script. If any one of your colliders is a trigger, then add OnTriggerEnter or else use OnCollisionEnter.

Unity OnCollisionEnter

OnCollisionEnter function is called if the colliders are non-trigger and one of them has a non-kinematic Rigidbody.

void OnCollisionEnter(Collision col)
    {
        If(col.tag=="Enemy")
           {
            Destroy(col.gameobject)
           }
    }

In this script we are using a normal collision. “col” returns the collider of the game object. We further check if the game object is an enemy. If yes, we destroy it. Remember either the parent Gameobject or the enemy game object needs to have a Rigidbody for this script to work.

Unity OnTriggerEnter

OnTriggerEnter function is called if the colliders are set as trigger and one of them has a non-kinematic Rigidbody.

void OnTriggerEnter(Collider col)
    {
        If(col.tag=="Enemy")
           {
            Destroy(col.gameobject)
           }
    }

Pro tip: If the object is marked as trigger, it will pass through the obstacles.

Other features in Unity OnCollisionEnter and OnTriggerEnter

Both the examples of Unity OnCollisionEnter and OnTriggerEnter above show that you can get the Gameobject of the colliding object. Apart from that you can get a lot of data from the collision class object. Here is the list of things that you can get from the collision class

  1. collider- get the collider of the colliding object
  2. relative velocity- get the relative velocity between the colliding objects.
  3. transform- get the transform of the object hit.

Other Unity Collider functions

That summarizes the collider function in Unity. If you have any questions, you can post it in the comment box below.

There are few more functions to detect whether the Gameobject has exited the collision phase or its still colliding with another object.

Non-Trigger Collision

  • OnCollisionExit- Is called when the Collider exits a Collision.
  • OnCollisionStay – Is called when the Collider is still colliding with another object.

Trigger Collision

  • OnTriggerExit- Is called when the Trigger Collider exits a Collision.
  • OnTriggerStay- Is called when the Trigger Collider is still colliding with another object.

Unity Collision Detection 2D

Collision in 2D is very similar to 3D, but there is a change in terminology when it comes to 2D. Unity has put in a lot of effort to distinguish 2D colliders from 3D.

2D Collision in Unity uses Physics2D. Everything in Physics2D has a suffix 2D added to it. It’s a physics engine on its own but for the 2D world.

Rigidbody2D

Rigidbody2D works very similar to Rigidbody but it reacts only to the components which are part of Physics2D. You can set a Rigidbody2D as Kinematic and also decide whether it reacts to physics forces or not.

To add a Rigidbody2D, select your 2D Gameobject and go to the inspector window. Click on Add component and select Rigidbody2D.

Rigidbody2D component

As you can see the parameters of Rigidbody2D are slightly different compared to a regular 3D Rigidbody. You can set the Rigidbody type as Static, Dynamic or Kinematic based on how your Gameobject will behave in your game.

Also, in 3D Rigidbody the physics material needs to be set from the collider but in case of Rigidbody2D, you can set the material from Rigidbopdy2D.

Adding a 2D collider

Select your Gameobject and click on Add Component in the inspector window. Add a 2D collider from the component list.

Use this comparison table and add the required collider.

3D collider2D Collider
OnCollisionEnterOnCollisionEnter2D
OnTriggerEnterOnTriggerEnter2D
OnCollisionExitOnCollisionExit2D
OnTriggerExitOnTriggerExit2D
OnCollisionStayOnCollisionStay2D
OnTriggerStayOnTriggerStay2D
ColliderCollider2D
CollisionCollision2D
RigidbodyRigidbody2D
BoxColliderBoxCollider2D
MeshColliderPolygonCollider2D
SphereColliderCircleCollider
CapsuleColliderCapsuleCollider2D
TerrainColliderEdgeCollider2D or TilemapCollider2D
WheelColliderCircleCollider2D

2D Collision layer setting in Unity

Similar to 3D you can decide which layers will collide with each other in physics 2D.

To set this you have to go to Edit>Project settings>Physics2D

Layer Collision matrix 2D

Functions in Unity Collision 2D

OnCollisionEnter2D

This can be used to detect collision between two colliders that are not set as Trigger and at least one of them has a Rigidbody attached to it.

Syntax

void OnCollisionEnter2D(Collision2D col)
{
//Your code
}

OnTriggerEnter2D

OnTiggerEnter2D is used to detect collision between 2D collides that are set as a trigger. You need to use this even if one of the Collider is set as Trigger.

Syntax

void OnTriggerEnter2D(Collider2D col)
{
//Your Code
}

Other Unity 2D collider functions

Non-Trigger 2D Collision

  • OnCollisionExit2D- Is called when the Collider2D exits a Collision.
  • OnCollisionStay2D – Is called when the Collider2D is still colliding with another object.

Trigger 2D Collision

  • OnTriggerExit2D- Is called when the Trigger Collider2D exits a Collision.
  • OnTriggerStay2D- Is called when the Trigger Collider2D is still colliding with another object.

Sample script for 2D collision detection functions

using UnityEngine;

public class collision_test : MonoBehaviour
{
    void OnCollisionEnter2D(Collision2D col)
    {
        Debug.Log(" Non-trigger Colliders entering Collision");

    }
    void OnCollisionStay2D(Collision2D col)
    {
        Debug.Log(" Non -trigger Colliders in Collision");
    }

    void OnCollisionExit2D(Collision2D col)
    {
        Debug.Log("Non-Trigger Colliders existing collision");
    }
    void OnTriggerEnter2D(Collider2D col)
    {
        Debug.Log(" trigger Colliders entering Collision");

    }
    void OnTriggerStay2D(Collider2D col)
    {
        Debug.Log(" trigger Colliders in Collision");
    }

    void OnTriggerExit2D(Collider2D col)
    {
        Debug.Log("Trigger Colliders existing collision");
    }

}

If you have any other questions regarding Unity collider then leave them in the comment box below.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Why Creativity is must for game developers How to become a Unity developer? How to Become a Game Developer? VR Game dev Companies: Top 10 Programming Languages used by Unity