OnCollisionEnter Vs. OnTriggerEnter — When to Use Them?

Unity has two ways of handling collisions:

OnCollision- When an object will physically hit another object and react somehow.

Since OnCollision contains physics information, Unity calculates the velocity and friction of objects that hit each other.

Using OnCollisionEnter

This is not all that useful for my game because messing with an object's physics will likely throw everything off.

OnTrigger- When an object will pass through another object and ignore the physics.

OnTriggerEnter can be used for triggering events without using the physics engine. This will also be less taxing on the game, not calculating all the physics in the background.

First, before we can use OnTriggerEnter we need to enable ‘Is Trigger’ in the Unity inspector:

Enable Is Trigger in the Unity inspector
The physics engine is now ignored

We can use the OnTriggerEnter(Collider other) method and Unity’s tagging system to ask the enemies if they’re colliding with the player or the player’s lasers. Then we can tell the enemy destroy itself, and the laser prefab and damage the player.

Code for destroying the enemy and lasers

--

--

--

Learning to become a unity game developer

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

Recommended from Medium

Patching GCE VMs using GCP VM Manager (OS Patch Management)

Finding solutions through chance

My Journey as a Computer Science Engineer

Exporting Data PowerBI to SQL Server

This Is How We Implement Agile in Yuk-Recycle.

My bots are now homeless.

Next Level Todo-App: Using React, Typescript, Capacitor, and GCP.

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
Tacoma Johnson

Tacoma Johnson

Learning to become a unity game developer

More from Medium

The Best Way to Bake Lights/Choose Static/Dynamic Objects

Virtual Cameras

Day 5 of my journey to becoming a Unity game developer

git it on the page!