In this Answer, we will learn how to create engaging enemy interactions in Unity by implementing collision detection and destruction logic. This Answer will walk us through setting up collision-based destruction, an essential element in many game genres.
This Answer is part of the introductory series on how to build a first-person shooter game using Unity.
Let’s dive into what we’ll be creating together in this tutorial.
Now, let’s explore the steps for implementing the destruction of enemy GameObjects upon collision in Unity.
We will start by either creating a new Unity 3D project or opening an existing scene. We will ensure it includes a main camera and a directional light. We’ll build upon the scene from our “Implement a Martian-looking skybox” tutorial.
We can perform the following instructions to create an enemy:
In Unity, we will go to “GameObject > 3D Object > Cube” to create a cube primitive GameObject. Once created, we will rename this cube to “Enemy” using the “Inspector” window.
We will open the “Inspector” window while the enemy GameObject is selected. Then, we will find the “Tag” drop-down menu and choose the “Add Tag...” option. After that, we will create a new tag named “Enemy” and assign this tag to the enemy GameObject.
With the enemy GameObject still selected, we will access the “Inspector” window and click the “Add Component” button. Then, we will navigate to “Physics” and choose a collider, such as the “BoxCollider” or “SphereCollider.” Moreover, we will follow the same procedure to add a collider to the bullet GameObject.
We will select the enemy GameObject again and tweak the collider’s size and shape in the “Inspector” window for proper coverage. Apply similar adjustments to the bullet GameObject’s collider to ensure precise collision detection.
For collision detection, we will use the OnCollisionEnter
or OnTriggerEnter
method in a script attached to the bullet or enemy. The script should handle the logic for when these objects collide.
using System.Collections;using System.Collections.Generic;using UnityEngine;public class BulletHandler : MonoBehaviour{public float life = 3;void Awake(){Destroy(gameObject, life);}void OnTriggerEnter(Collider other){if (other.CompareTag("Enemy")){Destroy(other.gameObject);Destroy(gameObject);}}}
In the script above, there is a BulletHandler
that handles the lifespan of the bullet and its interaction with enemies. The bullet will automatically be destroyed after the specified life
duration, and when it collides with an object tagged as Enemy
. In that case, both the bullet and the enemy will be destroyed, simulating a bullet-enemy interaction in the game.
Lines 14–21: The OnTriggerEnter
method is a Unity callback triggered when another GameObject with a collider component collides with this bullet’s collider:
In the OnTriggerEnter
function, it checks if the other
GameObject has the tag Enemy
using the CompareTag
.
If the other
GameObject is an enemy, it destroys the Enemy
GameObject using Destroy(other.gameObject)
.
After that, it destroys the bullet GameObject (gameObject
) as well, using the Destroy(gameObject)
which indicates that the bullet will be destroyed upon collision with an enemy and, in the process, the Enemy
GameObject will also be destroyed.
Depending on our game’s mechanics, we might want to perform additional actions upon destroying the enemy. For example, we might want to increase the player’s score, play a victory sound, or trigger an event.
To make the Gameplay realistic, let’s add a cannon as our weapon.
Test the collision and destruction logic in our scene. Fine-tune the collision detection, destruction effects, and any additional actions to ensure smooth and satisfying gameplay.
Note: Click the “Run” button below to start the game. Once the game is rendered, interact with it by clicking on the “Output” screen. Alternatively, you can access the game by clicking the link provided after “Your app can be found at:.”
import React from 'react'; require('./style.css'); import ReactDOM from 'react-dom'; import App from './app.js'; ReactDOM.render( <App />, document.getElementById('root') );
Stay tuned for our next tutorial, which will cover implementing randomly-spawned GameObjects within confined boundaries.
Free Resources