My journey becoming a Unity game developer: OnCollisionEnter Vs. OnTriggerEnter — When to use them? Triple Shot Powerup Behavior implemented.
Objective: Using code to give the Triple Shot Powerup behavior moving downward and destroying it when moving offscreen. Also, demonstrating the differences between OnTriggerEnter2D and OnCollisionEnter2D when grabbing the Triple Shot Powerup and actual gameplay.
In the PowerUps script inside the Update() function, we’re going to make the Triple Shot Powerup move downward at 3 meters per second. To do this, create a new variable with a SerializeField, Private Access Modifier, Type Float name speed. The speed variable will have a default value of 3f set initially, but can be changed in the Inspector.
Next, inside the Update() function we’re going to use the powerup’s Transform to move down by using the transform.Translate() method. Inside the Translate() method, use the Vector3.down method to move the powerup downward by its X-axis. Also, multiply Vector3.down by the speed variable and the real-time of Time.deltaTime.
Now to create the collision between the Player and the Triple Shot Powerup, we’re going to start by using the OnTriggerEnter2D() function. Change the collision parameter name passed in to other as this will be easier to code with in mind. Next, check IF the other object colliding with this Triple Shot Powerup is the Player by its Tag using the CompareTag() method which is quicker and more efficient in retrieving the game object we’re looking for. If it’s the Player object colliding, use Destroy() method to destroy this powerup game object.
With OnTriggerEnter(), if we look at the collision happening between the Player and Triple Shot Powerup, notice that the collision appears to happen inside of each game objects colliders. With OnTriggerEnter() game objects are allowed to pass through each other when colliding when Is Trigger is turned on. This allows us to use the OnTriggerStay() and OnTriggerExit() functions in gameplay when we want certain actions done when we’re inside of another collider, or certain actions done when we exit out from that collider.
Take note that OnTriggerEnter() collisions aren’t accurate collisions as they can trigger when the 2 colliders just touch each other, or when the colliders are inside each other. With this in mind, the gameplay collisions can look like the game objects really collided, or can be slightly off were only the naked eye might catch the collision looking off. OnTriggerEnter() allow the game objects to move freely until they encounter a collision that triggers actions that need to be applied.
On the other hand we will use the OnCollisionEnter2D() function to setup collisions between the Player and the Triple Shot Powerup. OnCollisionEnter() is called when the collider/rigidbody begins to touch another rigidbody/collider. OnCollisionEnter() is passed the Collision class as this class contains information such as for example contact points and impact velocity. Also, collision events are only sent if one of the colliders has a non-kinematic rigidbody attached.
Change parameter name from collision to other. We will check IF the Player is colliding with the powerup using the Player’s Tag, but we will use the Player’s collider to get the Tag’s name. If it’s the Player colliding, destroy this powerup game object.
Before playing the game, we need to turn off Is Trigger on the Player’s and Triple Shot Powerup’s colliders because if we don’t, these objects will go through each other with no collisions happening. Play the game, and watch OnCollisionEnter() objects collides as soon as the 2 colliders touch. OnCollisionEnter() provides consistent collision results when used, but doesn’t give the best looking collisions as objects can collide to the naked eye before even touching each other.
In the EnemyBehavior script, we expanded the collisions to check not only for the Player, but for the Player’s lasers as well using OnCollisionEnter(). We’re using the code from the OnTriggerEnter() function for the enemy’s behavior in the OnColliderEnter() function. Make sure to use the other’s collider to find the Player’s and Laser’s Tags.
Playing the game we notice that every game object with colliders and rigidbodies have been affected by physics when colliding with other objects with colliders producing weird results in our game.
Therefore, with this space game we will use OnTriggerEnter2D() for our collisions and actions to be applied after colliding in our EnemyBehavior script.
And for our PowerUps, we will use OnTriggerEnter() as well.