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

Consuming Kubernetes deployed Microservice using Services

Alpine, Slim, Stretch, Buster, Jessie, Bullseye, Bookworm — What are the Differences in Docker…

DotVVM for Visual Studio 2022 is now available

Unblocking Power BI — Dashboard in a Day (Ep. 2)

Nix Build Caching Inside Docker Containers

Automated API Tests

How to manage a set of Async function in for — in block in Swift.

Adventures In High Availability Logging — Elasticsearch, Logstash, and Kibana (ELK) on Kubernetes

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

My journey becoming a Unity game developer: Sleeping Guard Interaction-Player NavMesh movement…

2.5D Phase1 Framework: Moving Platforms

Initializing Ads on the Main Menu

From Prototype to Work of Art