Collisions in Unity

Daniel Kirwan
3 min readMar 30, 2021

Every game you play will have collision detection and it is vital to get right. Are you having issues with your collisions in Unity? Do you have colliders on your game objects but they’re still not recognising that they have collided with another game object? Well, having a collider on a game object is not enough in Unity, at least one of the objects involved in a collision must have a rigid body component to enable Unity’s physics system.

There are two types of collisions in Unity and they are hard surface collisions and trigger collisions. Hard surface collisions would be used for physics-based objects like kicking a ball and trigger collisions which are used for non-physics-based collisions such as collecting coins in a platformer or an ammo pack. The distinction between whether your game object is a trigger or a surface collision is found in the collider component. The second option allows you to turn the trigger option on, the default is for surface collisions.

IsTrigger

OnCollisionEnter or OnTriggerEnter?

Now that is the question. These are the methods that you will use when detecting collisions in a 3D game in Unity. There are also 2D versions of the same methods but the focus right now is on the 3D versions.

Below you can see the setup for the OnCollisionEnter method. The code is inside the Enemy script and when the enemy detects a physics-based collision it will destroy itself.

But the way I have my game objects set up this will not happen as the Enemy and Laser have their colliders set to trigger.

No collisions detected

Now I have the same setup in the OnTriggerEnter method and we will check what will happen.

You can see here that the enemy has now detected the collision through the trigger method and destroyed itself. I also have a trigger method on the laser that destroys itself when colliding with the enemy. I could do both of these inside the enemy script but wanted it separate for the article.

If you’re working with a surface collision in Unity you should use the OnCollisionEnter method but if you’re looking for a collectible then use OnTriggerEnter. Another point to make mention of is the settings for the rigid body component. I have added a rigidbody component to the enemy and laser prefabs but I have disabled the gravity option which means that the object is not effected by gravity when the game is playing.

--

--