A Tale of Two Colliders-Triggers vs Collisions in Unity
Adding a Rigidbody in Unity is enough to enable physics but it isn’t if you want interactions between objects.
Any object that requires some form of interaction needs a collider component. There are many presets in Unity that you can use to fit your needs:
If we have a cube, we would most likely want to add a “Box Collider”:
Clicking on the button next to “Edit Collider” allows you to adjust the size of your collider:
The Collider option “Is Trigger” is what dictates whether the object should pass through or collide with another object.
An example of when you would want a trigger would be when you want to detect if a player has reached a certain point, but not prevent them from moving forward.
Conversely, you would uncheck the “Is Trigger” if you want a physical barrier, like a wall, which would stop another object.
Within a Unity script, both types have their own methods for accessing them.
For passive interactions in 3D, here are 3 methods you can use:
OnTriggerEnter is called when the overlap of colliders begins.
OnTriggerExit is called when the overlap of colliders ends.
OnTriggerStay is called while both colliders remain overlapped.
For physical interactions, there are also 3 methods:
In all circumstances, the “other” argument refers to the collider of the external object that is in contact with the object owning the script.
A use case example would be if you wanted to destroy an object that comes into contact with the player: