Objective: Understand the basic difference between colliders and triggers
As said in the article about enemy behaviour, there are two ways of seeing collision in Unity, one we may call hard and a soft one. Let’s quickly recall the difference:
- A projectile ricochet on a surface or a ball bouncing on the floor are considered hard collisions.
- A player collecting a power up by colliding with it or an event triggered when the player touches an object are soft collisions.
Basically, hard and soft collision differ for the involvement, or not, of physics. In order to detect collisions in Unity, we have two possibilities: OnTrigger- methods and OnCollision- methods.
We already used OnTrigger, but let’s revise the requirements:
- Both gameobject must have a collider component
- At least one of them must have Is Trigger enabled in collider component and a rigid body component too.
The methods has a Collider object argument which allows to interact with the colliding object, because the gameobject and its components are accessible from it.
Inside the method we will put all the logic we want to run when the collision happens, both in the case of OnTrigger and OnCollision methods.
On the other hand, OnCollision methods have some special requirements:
- Both the object should have a collider component
- None of the objects should have Is Trigger enabled
- A rigid body component is required for moving gameobjects (so it may be necessary for both in some cases)
So basically, in order to have a call on OnCollision, the objects have to be colliders and not triggers, and the property is related to the bool Is Trigger.
Another big difference is in the argument of the method: OnCollision has a Collision (not Collider) object, which holds all information about collider, gameobjects etc., but also more physical information such as relative velocity, pulse exchanged in the collision and contact points of the collision, the latter will also provide the normal with respect to the contact point itself.
Which one should we choose? For which purpose?
First of all, Is Trigger value will tell which one you cannot use.
When your purpose is to realistically simulate physical effects of colliding objects , OnCollision is the choice, you can get properties of the point (or points) of collision and then Unity physics engine automatically resolves any changes in motion of the two bodies: for example the case of an object bouncing on another.
When Is Trigger is checked, you don’t have colliders anymore, but triggers: OnCollision methods will not be called, OnTrigger will.
In that case, the physics engine does not intervene and, for example, you can’t have triggers bouncing off each other.
So the short answer would be:
- Turn Is Trigger off to have colliders which obey laws of physics. Detect their collision with OnCollision methods.
- Turn Is Trigger on to have triggers (one or both) which won’t obey laws of physics and will be just region of space which, if entered/occupied/left by other triggers will cause scripted events.