Unity Guide
Implementing space shooter game features - Enemy view
A quick review of new features added to a space shooter game in Unity
Objective: Implement a way for enemies to detect power-ups and destroy them in a space shooter game with Unity.
In the previous post I implemented a smart behavior for some enemies in my space shooter game with Unity. Now it’s time to implement a way for the enemies to detect if they have a power-up in front of them and try to destroy it if that’s the case.
Creating the detector
In order to make an enemy identify the power-ups in front of it, let’s begin by dragging an enemy prefab into the scene view:
Next, let’s create a new empty object as the enemy child that will work as the power-up detector.
To be able to detect the power-ups, we’ll need to attach a 2D collider and a 2D rigidbody to the new empty gameobject. Let’s enable the Is Trigger option in the collider and let’s make the rigidbody a kinematic body type to avoid calling the OnTriggerEnter2D method of the enemy parent when something is detected in the collider:
Then, let’s modify the 2D collider to match the front view of our enemy prefab. This way, if a power-up (with a collider) enters the collider we can use the OnTriggerEnter2D method to detect it:
Now, let’s create a new script to handle the power-up detection and call the respective action when it happens:
Handling the detection
In order to indicate the enemy that a power-up has been found, let’s open the EnemyBehavior class and create a new public method that calls the shooting method of our enemy:
Now, let’s open the new script and create a new variable to store a reference to the parent with an EnemyBehavior class attached:
To initialize the variable, we’ll use the GetComponent method inside the Start method:
Then, to identify when a power-up enters the enemy view, let’s use the OnTriggerEnter2D method, which will compare the tag of the collider that triggered the method to check if it’s a power-up.
If a collider with a tag that belongs to a power-up is identified, we’ll call the respective public function using the EnemyBehavior class reference:
To make sure that the power-ups are detected, let’s add and select the respective tag into the power-up prefabs:
And finally, let’s open the Shot class and add the respective condition to detect when a shot is from the enemy and collides with a power-up to destroy both of them:
Now, if we try spawning several power-ups in the scene we’ll be able to see that the enemies shoot when they detect a power-up at the front to destroy it:
Spawning enemies with vision
Now, in order to spawn enemies that can detect the power-ups, let’s make the empty gameobject a prefab:
Then, let’s open the spawn manager script and add 2 new variables:
- Enemy view
This variable will store a reference to the new prefab that detects the power-ups.
- View probability
This variable will store the probability of an enemy to be able to detect the power-ups.
Now, thanks to [SerializeField], we can modify the variables through the inspector to choose the probability and drag the respective prefab:
Then, to spawn a new enemy with “vision” let’s compare the probability value and Random.value to instantiate the prefab that detects the power-ups as the child of every new enemy spawned:
And now, if we run the game in Unity, we’ll be able to see that some of the enemies detect the power-ups and shoot them:
And that’s it, we implemented an enemy that detects power-ups! :D. I’ll see you in the next post, where I’ll be showing more features added to my space shooter game in Unity.