OnTriggerEnter or OnCollisionEnter? When Should You Use Them & How?

OnTriggerEnter = Passthrough Collisions

This is called whenever an object with a collider passes through an object with a collider that has “isTrigger” checked.

OnCollisionEnter = Hard Collisions

OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.

[Note]

Both GameObjects must contain a Collider component. One must have Collider.isTrigger enabled, and contain a Rigidbody. If both GameObjects have Collider.isTrigger enabled, no collision happens. The same applies when both GameObjects do not have a Rigidbody component.

How Do I Use Them?

Well, since both OnTriggerEnter and OnCollisionEnter function in essentially the same way, the syntax isn’t really different. the real main difference is when to use them which is explained in the last section. So for this example, I will be using OnTriggerEnter to detect when an enemy object gets shot with a laser.

Example (Before Collision Script Added)

This script will be attached to the “Enemy“ game object.

Make sure to add the tag “Player” to the player object.

Example (After Collision Script Added)

Hi, my name is Adam Reed and I am a software engineer specializing in Unity and C# development. Feel free to scroll through and check out some of my work!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store