Detecting and Acting on a Collision in Unity

Simon Pham
3 min readJul 17, 2024

--

Why do we need collision detection in video games?

Collision detection is a significant part of almost every video game because it dictates many core features. For example, it affects whether or not a character can walk through walls or if the player’s projectiles can hit the enemies, etc.

Demonstration

Scene setup

To demonstrate how collision detection works in Unity, I set up a scene with a bunch of barrels.

The objective is to detect the collision between the red barrel and the yellow ones, and when the collision is detected, we want to add a small force to simulate an explosion.

Collision Detection

To detect the collision, we’re going to use the OnCollisionEnter method. This method requires the two colliding objects to have a Rigidbody component, so I have already added this component to each of the barrels in the scene.

Next, let’s create a C# script called “DetectCollisions” and attach it to the red barrel GameObject. For now, let’s print a message if the collision is detected.

using UnityEngine;

public class DetectCollisions : MonoBehaviour
{
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.CompareTag("Hazard"))
{
Debug.Log("Collision is detected");
}
}
}

In this method, the collider will be the object(s) that we hit (i.e., the yellow barrels), and we have access to some of its properties like name, tag, etc. Because all of the yellow barrels are tagged as “Hazard,” we can use the CompareTag method to check.

Now, let’s see the result.

As you can see, we have four messages logged to the console, indicating that four collisions have been detected.

Adding explosion force

To add an explosion force, we can use the AddExplosionForce method, which takes five parameters: explosionForce, explosionPosition, explosionRadius, upwardsModifier, and mode.

Read more about the method here

First, I’m going to declare five variables that represent the five parameters for the method.

private Vector3 _explosionPos;
private float _explosionForce = 10.0f;
private float _explosionRad = 3.0f;
private float _upwardsModifier = 3.0f;
private ForceMode _forceMode = ForceMode.Impulse;

The last four variables are pretty self-explanatory. For the explosion position (_explosionPos), we’re going to use the position of the red barrel.

Here’s the full code:

using UnityEngine;

public class DetectCollisions : MonoBehaviour
{
private Vector3 _explosionPos;
private float _explosionForce = 10.0f;
private float _explosionRad = 3.0f;
private float _upwardsModifier = 3.0f;
private ForceMode _forceMode = ForceMode.Impulse;

private void OnCollisionEnter(Collision collision)
{
_explosionPos = transform.position;
if (collision.collider.CompareTag("Hazard"))
{
Collider[] colliders = Physics.OverlapSphere(_explosionPos, _explosionRad);
foreach (Collider hit in colliders)
{
Rigidbody rb = hit.GetComponent<Rigidbody>();

if (rb != null)
{
rb.AddExplosionForce(_explosionForce, _explosionPos, _explosionRad, _upwardsModifier, _forceMode);
}
}

}
}
}

So when a collision is detected, we will cast a sphere at the explosion position and add an explosion force to each of the colliders.

And here’s the result:

--

--