Collision detection in Unity

Collision

So what is collision? Collision is knowing when one object has come into contact with another object. In this tutorial, we will look at collision detection, the Rigidbody component, colliders, triggers and physic materials. Scroll down to watch the video on all of these things or read on.

Rigidbody component

For objects to use Unity’s built-in physics engine, they need to include the Rigidbody component. To add it to a selected object, click Component > Physics > Rigidbody from the Inspector panel.

The Rigidbody component gives you several properties to work with including:

  • Mass
  • Drag (the amount of air resistance applied to an object when it is moving – an object with higher mass will need more force to move and will stop at a faster rate)
  • Angular drag (air resistance that is applied when turning)
  • Use Gravity
  • Collision Detection
  • and so on…

Collision detection

In order for objects to detect collision, they both need to use a component called a collider. A collider is a perimeter around an object that can detect when another object enters it.

Note: Objects don’t need rigidbodies to collide – all they need is a collider object. But rigidbodies allow objects to fall and are also needed for trigger collision – we’ll talk about that later.

Colliders

Objects like cubes, spheres and capsules already have collider components when they’re created. You can add a collider to an object by selecting the object and clicking Components > Physics from the Inspector panel, then select the collider shape you want such as box, sphere, capsule, wheel, terrain, or mesh (which takes the exact shape of a 3D model but can greatly reduce game performance).

When a collider is added to a game object the collider will appear in the Inspector panel for that object. Colliders have a number of properties including

  • Is trigger
  • Material (you can apply physic materials to change the way an object behaves – an object can behave like rubber, metal, wood, etc.)
  • Center (the centre of the collider
  • Size
  • and geometric properties eg. radius, if the collider is a sphere

You can try out different colliders with different shapes. For example, you can add a sphere collider to a cube so that it will roll around like a ball, and you can change the size of a collider so that the object either floats above other surfaces or sinks into other surfaces.

Watch the video below to see how you can work with the Rigidbody component, colliders, triggers and physic materials. Then scroll down to see more information on physic materials, triggers and the sample code.

Physic materials

To create a physic material, click Assets > Create > Physic material. You can then modify properties to make the object behave like metal, wood, rubber etc. For example, you can increase bounciness to make it behave like rubber. Once you have created a physic material you can add it to a collider attached to an object from the Inspector panel. Watch the video above to see how.

Triggers

Triggers can call three different methods that allow you to program what a collision event will mean. The three trigger methods are:

  • OnTriggerEnter – this is called when another object enters the trigger
  • OnTriggerStay – this is called when another object stays inside the trigger
  • OnTriggerExit – this is called when another object exits the trigger.

Triggers can be used to program what will happen if a player falls off a map, a player enters water, or an enemy enters a perimeter, for example.

If you added the code below to a script (called triggerScript) attached to a cube, it would display a message in the console whenever another object enters the cube:

using UnityEngine;
using System.Collections;
 
public class triggerScript : MonoBehaviour {
 
 void OnTriggerEnter (Collider other)
 {
 print("Another object has entered the collider");
 }
 
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 
 }
}

In this code, ‘other’ is referring to “another object”.

You could modify the code to say what specific object has entered the collider, for example:

using UnityEngine;
using System.Collections;
 
public class triggerScript : MonoBehaviour {
 
 void OnTriggerEnter (Collider other)
 {
 print(other.gameObject.name + " has entered the trigger.");
 }
 
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 
 }
}

Or you could even destroy the other object that has entered the trigger:

using UnityEngine;
using System.Collections;
 
public class triggerScript : MonoBehaviour {
 
 void OnTriggerEnter (Collider other)
 {
 Destroy (other.gameObject);
 }
 
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 
 }
}

Remember, you can replace the OnTriggerEnter method with OnTriggerStay and OnTriggerExit too. All of these things are covered in the video.

Why do objects fall through my terrain?

If you add have created objects with colliders that fall through your terrain that has a collider, you might want to disable the collider on your terrain and then use another collider attached to an empty GameObject. Click on GameObject > Create Empty an then attach a collider to that empty object. You can enable ‘Is Trigger’ on that object and then move the object and resize its collider so that it is positioned under your terrain.