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