Previously we discussed the basics of Unity’s physics engine. Now we will take a quick look at collisions.
Collisions are the events that happen in Unity when two or more objects in the scene that are equipped with Colliders and Rigid Bodies come into contact or overlap in space.
Unity provides us with two main types of collision methods that assist us in scripting behaviors and interactions:
I will preface this by saying that there are a few more methods related to Collider events both for 3D and 2D physics and I will briefly mention them later. For now, I want to focus on these two.
The main difference you may notice beyond their name is the parameter that each method takes.
- OnCollisionEnter() takes a Collision type parameter that can give us information on Contact points or the velocity at which the collision took place. This is because the event gets detected at the point of contact between two or more colliders. For this method to work, the object that detects the event must have a Rigid Body attached to it.
- OnTriggerEnter() takes a Collider type parameter. This returns information on the Collider that was detected. The location at which this collision is detected is not necessarily the first contact point. Basically, OnTriggerEnter() is used to return information about GameObjects that enter the collider’s space.
In this example, the “enemy” sphere is being moved down with a script component that uses
transform.Translate() . When it comes into contact with the “player” cube the enemy’s movement is halted. Notice how there is some jitter in the enemy’s motion while it stops on the player. The script that is moving the sphere is trying to apply its calculations to the GameObject attempting to move it down but the player object is constantly blocking it. A collision is occurring!
We can use this event to script some behavior. Let's assume that we want to destroy the enemy when it comes into contact with our player. We can make use of the
OnCollisionEnter() method in the following manner:
By placing this code in the enemy’s script it will run the
Destroy() line when it enters any collision.
The result is as expected: The enemy makes contact with the player, detects a collision, and is destroyed.
This same behavior can be achieved with
OnTriggerEnter() but some modifications will have to be made to our components.
For starters, there are specific conditions that must be met for Colliders to interact with each other. In the case of Triggers the main thing is to have a collider that is set to be a Trigger:
By selecting the check box, the enemy’s collider now acts as a Trigger, an area that does not act like a solid but can detect other Colliders. If we hit Play without changing anything else the result is as follows:
Under the hood a collision is actually taking place, two Colliders are overlapping in space but nothing else seems to happen. The enemy passes through our player without batting an eye. However, We can tell Unity to destroy the enemy with the
By replacing the code we previously added with this new version we can obtain the same results as before.
The enemy detects another Collider that enters its own Trigger and fires off the
OnTriggerEnter() method which in turn destroys it.
Both examples lead to the same result. So what’s the real difference? Mostly, I would argue, is what you use them for. Sometimes the need for one of the two methods will become obvious:
Say you want to create a dungeon adventure game where your player must face many traps. One of such traps might be a swinging log. In order to set off the trap, the player steps into a Trigger — a Collider in the game world that has no effect on the player’s position or movement. It does, however, make use of
OnTriggerEnter()method to set off the trap. The log comes swinging down and collides with your player. The
OnCollisionEnter()method can now be used to apply force and direction to your player based on the point in space where they both collided.
This is just skimming the surface of what you can accomplish with these two methods. Like I previously mentioned, there are a few more Collider events that we can use:
- OnCollisionStay(Collision other): is called every frame while two or more objects with Rigid Bodies and Colliders are touching.
- OnCollisionExit(Collision other): is called once when two objects with Rigid Bodies and Colliders stop making contact.
- OnTriggerStay(Collider other): is called intermittently while a Collider is contacting or overlapping the Trigger area.
- OnTriggerExit(Collider other): is called after a collider stops being in contact with a Trigger area.