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.

You’ll want to use collision detection like this when you are detecting the collision for the purpose of triggering some form of function such as dealing damage or picking up objects.

OnTriggerEnter happens on the FixedUpdate function when two GameObjects collide. The Colliders involved are not always at the point of initial contact.

OnCollisionEnter = Hard Collisions

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

Collision detection like this is used more for physics and objects that don’t allow things to pass through them. This can be useful if you need to detect when the player comes in contact with a wall or obstacle of sorts.

In contrast to OnTriggerEnter, OnCollisionEnter is passed through the Collision class and not a Collider. The Collision class contains information about contact points and impact velocity.

[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!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

5 Ways to Connect Redux Actions

Scrimba 24 Days of Javascritpmas — Solution day 01–17

10 Apps That Use React Native

Build a Paint App with es6 in 10 minutes!

Don’t Be Shy to Share Your Knowledge

Difference Between var, let and const in JavaScript

Prefer to define functions on the prototype not the instance in JavaScript

What’s wrong with the Search @ npmjs.com

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
Adam Reed

Adam Reed

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!

More from Medium

Scriptable Objects 1

Scriptable Object Script

Day 7: The Enemy

Setting up GIT for Unity

git it on the page!