Day 11 — OnTriggerEnter or OnCollisionEnter
Hey and welcome!
It’s time we start talking about collisions as they’re one of the most important aspects in game development that dictate numerous events and happenings within your game world, from checking to see if a bullet has collided with an enemy to do damage to them or a player colliding with a trigger that sets off a cutscene.
With this article we’re going to be checking for a laser object and our player object colliding with our enemy object and then destroy the enemy object when that happens.
Difference between a hard and trigger collision
First though we need to know which type of collision detection we’re needing, ultimately there are two types of collisions in Unity, your hard collisions and trigger collisions. A hard collision is the physical collision between one object and another, so if you threw a ball object at a wall object and it bounces off of the wall that would be a hard collision. With that logic a trigger collision is one that can pass through an object so the ball in this case would pass through the wall.
In either case we can use a method to check for the collision of our ball with the wall. If we want to check the hard collision then we would use this:
OnCollisionEnter(Collider other)
{
// Your code here that runs when a collision happens
}
Then a trigger collision would make use of this:
OnTriggerEnter(Collider other)
{
// Your code here that runs when a collision happens
}
Hopefully you’ve figured out that it’s the OnTriggerEnter method we’re going to be using since we’re not looking for our laser or player to bounce off of the enemy and instead want to check when their colliders cross into each other.
Firstly setup an enemy with its own enemy script/behaviour and get it to do the following with what we’ve learned:
The only fancy new thing happening here is that we’ve set the enemy to respawn randomly along the x axis when their transform.position is reset to the top when they hit the bottom of the screen. All you need though is Random.Range(-9.5f, 9.5f) in the x value when you’re setting the new Vector3 position and that will simply pick a random number between -9.5f and 9.5 and use that as the new x value.
Setting up the collision for our Enemy object
Right let’s get colliding, make sure that you have a rigidbody component on both your laser and enemy object as a rigidbody is needed on at least one of the colliding objects in order for a collision to work. Next let’s create two new tags of Laser and Enemy using the add tag option in the tag dropdown in the inspector and the assign those to the relevant object:
Then the fun bit, our code which we add to the enemy object making sure that it’s not in the Update() method as we’re adding in our own one here:
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
Destroy(this.gameObject);
}
else if (other.tag == "Laser")
{
Destroy(other.gameObject);
Destroy(this.gameObject);
}
}
The (Collider other) part of this is simply assigning whatever collides with our Enemy object that variable of other so other could be renamed to anything here if you wished. This though is where our tags finally come into play, in the first if statement the condition being checked here is if other.tag matches the “Player” tag we have on our Player object or the “Laser” tag on our Laser object.
So if the Player collides with the enemy they’re being assigned to the variable of other which means that other.tag would be the Player’s tag.
Then we have the Destroy() methods that we’re familiar with but one thing to mention here is that we could also be checking for the collision in the Laser script using OnTriggerEnter() in there and destroying the object in there. Luckily though we will already get a reference to the Laser object when it’s assigned to other so that we can destroy the Laser by using other.gameObject which works just as well.
With that you should have something looking like the above, now you got a game forming, hope you’re excited as I am!