Road TO Dev — OnCollision vs OnTrigger

Ok so we have our object all setup and have our scene set! We already know how to move a character so setting that up should be easy just have to direct it’s motion towards what you want to collide with. Once that is done you just need to code it in. Now when setting up collisions there are a few options Enter, Exit, and Stay. Depending on your intended effect you need to decide when to trigger your collision after it hits the object. For my purpose here I will be activating the collision on Enter meaning as soon as the two objects colliders interact my action will happen.

What you see here is whenever my object collides with another collider it will spawn the object I’ve told it to and then destroy itself. That looks like this.

Next let’s talk about OnTrigger. Essentially when you have the trigger box enabled this takes the collision and makes something you dictate happen when the objects collide. It won’t react like they are physical forces against each other as with Collision. Think of a power up you get in a game the object is there you run over it but don’t actually “hit” it yet you get the powerup. That’s using Trigger.

All you need to do is tell the character that when it hits the collider an object it needs to do something. This is very easy in Unity using the tag system because when you hit an object you get information form that object and as long as you setup your tag correctly you can easily access that information or even do something to that object as seen here when the cube hits the sphere I kill the sphere from the cubes script.

See it really doesn’t take much to create a collision. Remember that gravity tick I was talking about? Well I gave my wall and floor colliders so even though the code for movement on my object doesn’t change this is what gravity does.

Physics can be fun but remember when you do things like what I am above creating objects on impact that takes memory to do! And if you get the nice idea to spawn many physical objects that have the same properties it can have unintended issues… like completely freezing unity. Remember Save often!

Mistakes were made… with Unity! :D

Alright that wraps up the basics for OnCollision and Trigger. There is a lot that can be done with these tools and experimenting IS fun.




Hi there! I am currently on the path to learn how to become a unity developer and will be documenting my journey as best as I can here. Thanks for looking!

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

Recommended from Medium

Implementing Master/Slave architecture in Jenkins — Tutorial

Wikilect: Doing full-scale knowledge graph with RDF and Virtuoso database — easy-peasy or not…

How to Rotate Proxies in Scrapy

How to Setup Jenkins and Integrate NUnit Selenium C# Framework?

Setup Jenkins

Using NATS to Implement Service Mesh Functionality, Part 3: Metrics, Tracing, Alert, Observability

Slack + JIRA + Zapier = ❤

Bitbon System Decentralization Roadmap

How To Get Sugar Rates API In Python

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
Rob Smith

Rob Smith

Hi there! I am currently on the path to learn how to become a unity developer and will be documenting my journey as best as I can here. Thanks for looking!

More from Medium

From Prototype to Work of Art

We all start somewhere

Lerp Squares — Part 3

What is Filebase and How do you use it?