Unity Collider : Collision in Unity simplified

Unity Collider is one of the most important parameters or components of any gameobject. Collisions are used in almost all the games made with unity or any other engine. Without the basic understanding of how unity Colliders work, it will be very difficult to code the game mechanics.

If you are totally new to Unity check out how to learn Unity.

Unity colliders are very simple to use. First thing that you have to pay attention to is unity has divided the colliders into 2D and 3D. So you have to very careful which collider are you using. Further, there are types, static collider, rigidbody collider, kinematic rigidbody collider.

Unity Collider

Unity Collider Types

Static collider

Static colliders are considered to be non moving objects by unity. Do not confuse static gameobject with static collider. If rigidbody is not attached to a collider then its a static collider. They do not move when an object Collides to with it. Though, they can be moved using transform, moving a static collider leads to performance loss during runtime.

Rigidbody 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 an kinematic rigidbody compared to a Static object.

On Collision enter vs On trigger Enter

Unity collision: on collision enter

Unity will detect a collision only if one of the object is a rigidbody. That is, if you have two gameobject marked as static collider then collision between them will not be detected. Its the same case with two kinematic rigidbody. If you use OnCollisionEnter function in any of the above case, the function will not be called during a Collision. Find the collision matrix for unity colliders in the image below.

collision matrix for unity colliders

Ref:unity docs

Unity Collider: On trigger Enter

When you mark an collider as 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 other object space can trigger a function. With that you can know if an collision has happened without actually creating a collision. You have to use OnTriggerEnter function if the collider is Marked as a trigger. The collision matrix is a little different in case of triggers.

Trigger collision matrix

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

Sample Unity collision script

On collision enter

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.

On Trigger Enter

We can do the same thing with trigger with this code.

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 obstacle and no collision physics will act.

2 thoughts on “Unity Collider : Collision in Unity simplified”

Leave a Reply

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

%d bloggers like this: