# Colliders

Build on the fundamentals of physics by learning about the Collider component.

We'll cover the following

## Overview

The “Collider” component defines an invisible boundary around our GameObject (irrespective of its mesh). This boundary is treated as the physical body of the GameObject by the physics engine in the event of a collisionThe phenomenon when two or more collider boundaries overlap with each other.

In the example below, the red sphere has a “Sphere Collider” as well as a “Rigidbody” attached to it, and the green platform has a “Box Collider” attached to it. The boundaries of both the Colliders overlap with their respective mesh boundaries.

Therefore, the collision between the two objects seems perfect, as depicted below:

Now, let’s try decreasing the size of the “Sphere Collider” so that it becomes enclosed inside the mesh of the red sphere.

As we can observe in the scene below, it seems as though the red sphere gets stuck inside the green platform. However, in reality, the Colliders are on top of each other, rather than being stuck inside one another.

## Physic Material

In the previous examples, you must have noticed that the red sphere simply rests on the green platform on impact, but what if you wanted the sphere to bounce, just like a rubber ball? The “Physic Material” is one such provision in Unity physics that allows us to associate some properties with the Collider. To generate a “Physic Material,” we’ll navigate to the “Project window > right-click Create > Physic Material” and modify the resulting asset as illustrated below:

The “Physic Material” above has a “Bounciness” of 0.8 (defined on a scale of 0 to 1), which will allow the GameObject to exhibit a rebounding behavior on collision. If we attach the “Bouncy Surface” to our red sphere’s Collider by simply dropping it from the “Project” window to the Collider’s “Physic Material” property, we’ll get the following behavior:

## Primitive colliders

Primitive colliders are very performant Colliders because they are equivalent to geometric figures. Therefore, they’re less computationally expensive. In total, there are three primitive colliders, namely the Box Collider, Sphere Collider, and Capsule Collider. We’ll discuss them in detail below.

### Box Collider

The “Box Collider” is a cuboid-shaped collider. We can alter its dimensions via the “Size” property and change its position in the GameObject using the “Center” property.

### Sphere Collider

The “Sphere Collider” is a sphere-shaped Collider. Similar to the “Box Collider,” we can change its position in the GameObject using the “Center” property and also alter the size using the “Radius” property.

### Capsule Collider

The “Capsule Collider” is a cylinder with half spheres at each end. We can change the Collider’s position via the “Center” property and adjust the size of the spheres using the “Radius” property. Moreover, the “Direction” changes the axis of the cylinder.

## Mesh Collider

The “Mesh Collider” is a special type of Collider that can be used for GameObjects with a complicated non-geometric mesh. The “Mesh Collider” takes the respective mesh asset and uses that to build a Collider with exact mapping. In the example below, the “Mesh Collider” component uses the “Car 3” mesh, the same as the “Mesh Filter,” to build the Collider.

Even though a Mesh Collider results in more accurate collision detection, it takes a toll on the processor. Moreover, there are several downsides to using Mesh Colliders.

Mesh Colliders can collide with other types of Colliders. However, they can’t collide with each other, as shown below:

However, if we enable the “Convex” property on one of the two Mesh Colliders (in this case, the yellow car), they’ll be able to collide.

However, the "Convex" property alters an object's collider to ensure it is convex. This change simplifies the collider, enhancing the efficiency of collision detection. Yet, this simplification might cause the collider to not precisely match the object's actual complex shape. Consequently, it can lead to less accurate collisions.

Moreover, a Rigidbody can’t be attached to a non-convex Mesh Collider. Therefore, they can’t be affected by forces (this is the reason that only the yellow car was affected by the collision and the red car wasn’t).

So, is there an alternative to the Mesh Collider that’s less processor-intensive and covers a complicated mesh?

## Compound Colliders

The “Compound Collider” refers to using multiple primitive colliders for a single GameObject. We can either add all primitive colliders to a single GameObject or add them in child GameObjects, as shown below:

Note: The Rigidbody must be attached with the root GameObject rather than with a child GameObject to ensure that the complete GameObject is treated as a single body, irrespective of Colliders being on different child GameObjects.

## Collider methods

Colliders not only enable collision between GameObjects, but also allow us to define what happens as a result of the collision via the following callback methods:

• OnCollisionEnter: This method is invoked when the physics engine detects the collision for the first time.

• OnCollisionStay: This method is invoked if contact between the GameObjects is still intact.

• OnCollisionExit: This method is invoked when the contact ends.

In the following example, the blue cube shrinks in size when the cubes collide.

The script below was attached to the blue cube. Can you guess how it works?

Press + to interact
using System.Collections;using System.Collections.Generic;using UnityEngine;public class Shrinker : MonoBehaviour{    private void OnCollisionEnter(Collision collision)    {        gameObject.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);    }}

If you’re unsure about the workings of the script above, feel free to press the “Show Explanation” button:

## The IsTrigger property and methods

The “Is Trigger” property makes the Collider pass through. In the animation below, the blue cube’s Collider’s “Is Trigger” property is turned on; therefore, the red cube passes through it.

This property is useful because, like normal colliders, Trigger Colliders have special callback methods that allow us to control the collision behavior:

• OnTriggerEnter: This method is invoked when the physics engine detects the collision for the first time.

• OnTriggerStay: This method is invoked if contact between the GameObjects is still intact.

• OnTriggerExit: This method is invoked when the contact ends.

In the example below, the blue cube’s Collider has the “Is Trigger” property turned on, and the blue cube shrinks once the red cube exits its collider.

The script below was attached to the blue cube. Can you guess how it works?

Press + to interact
using System.Collections;using System.Collections.Generic;using UnityEngine;public class Shrinker2 : MonoBehaviour{    private void OnTriggerExit(Collider other)    {        gameObject.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);    }}

If you’re unsure about the workings of the script above, feel free to press the “Show Explanation” button:

## Types of Colliders

Since we have studied a lot about Colliders and their properties, let’s now get familiar with the terms Unity uses to refer to GameObjects that have specific Collider and Rigidbody configurations. The following table summarizes the terms:

## Collider Types

 Name Rigidbody Is Kinematic Is Trigger Static Collider No N/A No Dynamic Collider Yes No No Dynamic Kinematic Collider Yes Yes No Static Trigger Collider No N/A Yes Dynamic Trigger Collider Yes No Yes Dynamic Kinematic Trigger Collider Yes Yes Yes

## Collision matrix

The tables below list when a collision is detected and relevant callback functions are invoked:

## Non-Trigger Collision Detection Matrix

 Static Collider Dynamic Collider Dynamic Kinematic Collider Static Collider No Yes No Dynamic Collider Yes Yes Yes Dynamic Kinematic Colldier No Yes No

A common mistake as a beginner is to expect that a collision gets detected between two static colliders (GameObjects with a simple non-trigger Collider and no Rigidbody).

## Trigger Collision Detection Matrix

 ﻿﻿﻿Static Collider ﻿﻿﻿Dynamic Collider ﻿﻿Dynamic Kinematic Collider Static Trigger Collider Dynamic Trigger Collider Dynamic Kinematic Trigger Collider Static Trigger Collider No Yes Yes No Yes Yes Dynamic Trigger Collider Yes Yes Yes Yes Yes Yes Dynamic Kinematic Trigger Colldier Yes Yes Yes Yes Yes Yes

In the case of trigger collisions, the interaction between Static Trigger Colliders and static colliders goes unregistered by the physics engine.

Congratulations! You’ve now learned the crux of Unity physics.