Unity Guide

Implementing space shooter game features - Extra Life

A quick review of new features added to a space shooter game in Unity

Fernando Alcantara Santana
Nerd For Tech

--

Objective: Implement a health power-up item for a space shooter game with Unity.

In the previous post I implemented a strength visualizer for the player’s shield in my space shooter game with Unity. Now it’s time to implement a new power-up item that provides health or an extra life.

First, like we did in an older post, let’s create the power-up item by dragging the respective sprite into the scene:

Then, let’s add the components we need inside the inspector (Collider, Rigidbody and Powerup script):

As there are already 3 other power-ups, the extra life will take the number 3 as the power ID in the Powerup script.

Now let’s drag it into the respective folder to create its prefab:

Spawn Manager script

Then, select the Spawn Manager and add the prefab into the array of powerups that we created before:

This is the array in the Spawn Manager script.
The array will have 4 power-ups now.

Then, in the Spawn Manager script, lets add the respective number of powerups in the script so that the random spawning includes it:

You could also use the array.Length value to avoid using hard-coded numbers.

Powerup script

Now let’s open the Powerup script and add the respective call in the switch statement that checks the power ID when the power-up collides with the player:

The 2nd parameter indicates how much time the power-up will last. A negative value indicates that there’s no need to deactivate the power-up.

Player script

Then, let’s open the Player script and add the power-up case in the EnablePowerup method. We’ll add the player an extra life and then update the number in the UI when enabling the power-up:

UI Manager script

Now, to indicate when the player gains an extra life, let’s create a new Text element inside our canvas and let’s put it after the lives displayer:

The Text element will indicate the number of extra lives that the player has collected.

Then, in the UI Manager script, let’s create a new variable to store the reference to the Text element:

Use [SerializeField] to be able to drag the value form the inspector.

Now, in the inspector, drag the Text element into the UI Manager script component:

Finally, let’s add the respective condition in the function that updates the lives in the UI Manager to display the extra lives using the new Text element:

Now, if we run the game, we’ll see that the extra lives are displayed when the player collects the extra life power-up item:

Player script

And now, in order to make the new power-up item have a healing effect, we can deactivate a damage visualizer in the player every time that it collects an extra life. Let’s add a new function in the Player script to handle it:

Using return will end the function to avoid searching for more elements in the array.

Then, let’s add the respective call after the power-up was collected:

If we run the game, we’ll see that the damage visualizer is deactivated when the player collects the power-up item:

And that’s it, we implemented a health power-up item! :D. I’ll see you in the next post, where I’ll be showing more features added to my space shooter game in Unity.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D

--

--

Fernando Alcantara Santana
Nerd For Tech

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).