Refilling Player Health
Welcome back! I’ll be continuing to implement the requirements for the first phase of the 2D games certification in the GameDevHQ Professional Unity Developer Program! Todays requirement is to create a new collectible object for the player that will allow them to regain some of their health. The requirement reads as follows:
- Create a health collectable that heals the player by 1. Update the visuals of the Player to reflect this.
Create and Import the sprite assets
Like with the Ammo Powerup, creating a new powerup required some new graphics to be created. I, again, took the existing sprites for the shields powerup into photoshop, changed the text to read “HEALTH” and shifted the colors with a filter. Then I re-exported them and imported them into my project under their own folder.
Create the Health Collectible Game Object
I follow the same process that I did with the Ammo collectible to create the Health collectible game object. I select all of the imported sprites and drag them into my hierarchy window in the project Unity to create a new animation and GameObject at the same time. Then, I add a CircleCollider2D and a Rigidbody2D for pickup collisions. I also add a new value to the PowerupType enum for health.
Next, I add the Powerup script to the object and set the power up type to the newly added “Health” powerup type. I also set the Powerup Duration to 0 seconds since this powerup’s effect is instant. At this point, I prefab the object by dragging it into my Resources > Prefabs > Powerups folder.
Player script modifications
In the player script I add a variable for max lives, default it to 3, and expose it to the editor, I then set the players lives to the value of max lives in the Start method. Lastly, I add the Health case to the switch statement that processes powerups. I add code to check to make sure that the player isn’t dead (more than 0 lives) or at full lives and give them 1 more life if they are in-between those requirements. I then call the game manager to update the players lives, update the player’s damage indicators, and play the sound for picking up a powerup.
With that, our game has a new powerup to restore life for the player!
In this article we added a new powerup item for the player to pick up that refills their health. Next time, we’re going to add a collectible that adds a new type of weapon for the player to fire! If you enjoyed this article, or want to come along with me as I progress on my journey, follow me at gamedevchris.medium.com.