Day 6: OnCollisionEnter Vs. OnTriggerEnter
Today's topic is collisions between two objects, Unity provides two ways to handle this problem, we must specify one thing, two objects can collide with each other or through each other without something happening between them.
To make this possible game objects must have a Rigidbody assigned between the components.
Another essential element is the collider, there are different types of collider, cube, sphere, capsule or that follow the mesh of the model.
We can see that already in the collider box we have an “isTrigger” entry that allows us to decide if our game object should have collisions or can pass through objects.
Now we see the difference between these two methods.
if Is Trigger is true
Game objects are passed through, but with the call of the OnTriggerEnter method, we can recognize a specific object and assign it a behavior to execute.
In my example, the result is this
if Is trigger is false
If instead, we want to use collisions, the call remains similar but we go to look for the collision directly instead of the collider.
We need the “is trigger” entry to be false.
If you don’t handle the OnCollisionEnter method here is what could happen inside your game.
It could be a nice and very relaxing screensaver.