loading words...

May 21, 2019 21:46:16

Colliders and Triggers in Unity

by @valentino | 302 words | 273🔥 | 273💌

Valentino Urbano

Current day streak: 273🔥
Total posts: 273💌
Total words: 132759 (531 pages 📄)

Unity uses colliders to detect hits between different objects in the scene. If you have 2 cubes in the scene and you move one of top of the other nothing will happen. If instead you add a collider to both of them they will push each other around instead of stacking. Using a proper collider is a good advice for performance. It won't matter if your scene is small and only has a few gameObjects, but once they start increasing the choice of a more computationally heavy collider will hit your performance hard. On the other hand the choice of a poor collider might make your collision detection less precise or even miss the collision entirely.

You can also receive a callback on collisions so that for example in a shooter you can decrease the HP of the hit target.

```

void OnCollisionEnter2D(Collision2D other)

{

}

```

When you use a collider you have the choice of setting it as a trigger. If you do the collider will no longer react to other colliders, but instead it will act as an event so that you can react to it from code.

An example would be for powerups: I want to know when the player steps on a start so that I can add the power up to the player character. Using a trigger I can implement the function "OnTriggerEnter" or "OnTriggerEnter2D" in case of 2D colliders.

```

void OnTriggerEnter2D(Collider2D other)

{

}

```

For testing purposes I wanted to receive a callback in both cases, simply add the following script to the gameObject you want to track. Create a file called TestCollision2D.cs and paste:

```

using UnityEngine;

using System.Collections;

public class TestCollision2D : MonoBehaviour

{

void OnCollisionEnter2D(Collision2D other)

{

Debug.Log("Player collided hit");

}

void OnTriggerEnter2D(Collider2D other)

{

Debug.Log("Player collided trigger");

}

}

```

Originally published at www.valentinourbano.com

contact: email - twitter / Terms / Privacy