My journey becoming a Unity game developer: Enemy prototype for Galaxy Invaders pt.2

Rhett Haynes
3 min readJun 15, 2022

Objective: Setup the Enemy to be destroyed when they collide with the Player or Laser objects. These actions will be performed using Colliders, Rigidbodies, and the OnTriggerEnter() function.

Enemy destroyed by the Player and Laser on collision.

We need to create collisions for the Enemy with the Player and Laser game objects. For this to happen we 1st need to Turn On the Is Trigger on the colliders of the Enemy prefab, Player, and Laser prefab. This will allow these game objects to not only collide with each other, but pass through each other compared to bouncing off each other if Is Trigger is turned off.

Also, for collisions to happen we need to have at least 1 game object with a Rigidbody component attached to it. Rigidbody component enable your game objects to act under the control of the Unity’s physics system. A game object must contain a Rigidbody component if it want be influenced by gravity, act under added forces through scripting, or interact with other objects which in this case will be colliding with other objects. We will add Rigidbody components to the Enemy and Laser prefab game objects as they will be involved in collisions the most. Turn off Use Gravity on the Enemy and Laser prefabs since we don’t want gravity applied to them.

Rigidbodies added to the Enemy and Laser prefabs, and Is Triggers turned on for their colliders including Player.

In the EnemyBehavior script, add the OnTriggerEnter() method and use the Debug.Log() method to test for any collisions with the Enemy object. Use the other transform name to see the name of the other objects colliding with the Enemy.

Using OnTriggerEnter() function to test for collisions with the Enemy object.

Play the game, collide into the Enemy with the Player and lasers, go to the Console to see the results of which objects collided with the Enemy and how many times.

Enemy is colliding with the Player and Laser objects.

To be more precise in finding the Player, Enemy, and Laser objects we’re going to give each of them Tags. These Tags will be given specific names that will make it easy to find them. The Player tag has a default tag created in the drop-down list under Tag that can be used. However, the Enemy and Laser must be created using Add Tag. Make sure to add these tags to the prefabs in the Prefabs folder because if not, you must use the Overrides drop-down list to Apply these changes to the prefabs.

Tags added to the Enemy, Player, and Laser.

Inside the EnemyBehavior script under OnTriggerEnter(), check IF the other object colliding is the Player by using CompareTag() method. The CompareTag() method is more efficient in finding game objects. If the Enemy collided with the Player, Destroy() this game object which is the Enemy itself.

Also, check IF the Enemy is colliding with the Laser using CompareTag() as well. If it collided with the Laser, we need to Destroy() this Enemy game object and the other game object which is the Laser as well.

Destroying the Enemy object when colliding with the Player and Laser objects.

Enemy is now being destroyed by the Player and Laser objects in the game.

Enemy destroyed by the Player and Laser on collision.

--

--