COLLISONS: OnCollision or OnTrigger — the difference and when to use them.

Yesterday we spoke about physics inside of Unity where I explained how to add them. Today we will dig a little deeper and I’ll show you how to code in the collision depending on whether you want a hard collision or on trigger.

Hard Collision — OnCollisionEnter.
This is where you want the objects to bounce off each other (like in the gif above). This will tell Unity to use physics information such as Mass and Velocity. If one object has more Mass it is going to be the winner, like that ball guy on the right in the gif above! Velocity will work the same, the more velocity an object has the harder it will hit.

Trigger Collision — OnTriggerEnter.
This is where you want the object to “absorb” the other. For example, Mario colliding with a coin. The coin will disappear and Mario’s movement will not be affected. You need to have the On Trigger box checked in your Collider in the Inspector for this to work.

Really, it’s that simple. Of course you will need some code behind these to work, but that is a story for another article — like how about tomorrow!

Tomorrow we will look at communicating between scripts. So to use collision for an example, if there is a collision take a life away from the player. The collision code will be inside the Enemy for example and if they collide you need to communicate with the Player script and subtract a life. You won’t want to miss it, it is a VERY important part of creating a game!

Happy clicking!



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