Unity Guide
Implementing space shooter game features - Extra Life
A quick review of new features added to a space shooter game in Unity
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):
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:
Then, in the Spawn Manager script, lets add the respective number of powerups in the script so that the random spawning includes it:
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:
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:
Then, in the UI Manager script, let’s create a new variable to store the reference to the Text element:
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:
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.