Now that we understand basic collisions in Unity3D, let’s create an Enemy GameObject that can be destroyed by our Player GameObject.
First, we will create a new Cube GameObject by right-clicking in the Hierarchy window and renaming it to Enemy. Next, we will create a C# Script called Enemy, and attach it to our Enemy GameObject.
If we look in the Enemy GameObject’s Inspector window, we see that there is already a Box Collider component attached to the Enemy GameObject. For this example, we will want the Enemy GameObject to be destroyed when the Player GameObject hits it, so we will need to turn the Enemy GameObject into a Trigger by clicking Is Trigger.
Next, we will need to add a Rigidbody component to the Enemy GameObject and the Player GameObject, and uncheck Use Gravity so our GameObjects do not start falling on their own.
We will also be creating a downward movement for the Enemy GameObject, so we will want to move our Enemy GameObject to the top of our screen. This way we have time to shoot the Enemy GameObject, but it will also be able to fall into our Player GameObject to test our collision event.
Open the Enemy script, and we will create the downward motion and set up the OnTriggerCollision method for the collision events.
Within the Update method, we will use a similar movement statement for the Enemy GameObject as we did for the Laser Prefab, only now, the movement direction will be down with a speed of 5-meters per second.
Next, we will use the OnTriggerEnter method to set up our collision event.
Since we are in the Enemy script, we will need to know what object collided with us, and that information is stored in other. To verify this information, we can use Debug.Log and find the name of the GameObject that hit the Enemy GameObject.
If we save our script and run the scene in Unity, we can see in the Console window that when the Enemy GameObject and the Player GameObject collide, we see the message “Who hit us: Player” in the console. The same as well for the Enemy GameObject colliding with Laser Prefab, we see “Who hit us: Laser”. We now have successful collision detection.
Next, we can write out our Pseudocode for what we want to happen after the collision. For this example, we will just be focusing on what happens to the Enemy GameObject when it collides with other, and in this case, other is the Player GameObject. Our Pseudocode will look like this:
We will be using an If statement and we need to define other. The way we define other is by using Tags for our GameObjects. If we go back into Unity and look at the Player GameObject’s Inspector window, we will see the Tag property located below our GameObject’s name.
Currently, our Player GameObject does not have a Tag, so we will need to click the Tag dropdown and select Player from the list.
There are a few preset Tags like the Player Tag, but if we need to create a custom Tag for our Laser Prefab or our Enemy GameObject, we would click Add Tag… at the bottom of the list and create the new Enemy Tag.
Now that we have a Tag reference for our Player GameObject, we can use it to define other in our If statement.
Basically, the code reads: if the tag of the other GameObject that hit us is “Player” (which is a string), then we need to destroy this gameObject.
If we save this script, go back into Unity, and run our scene, we will see the Enemy GameObject disappear after it collides with the Player GameObject.