Mr Fixit — Adding a Repair Powerup
Welcome back! Today we’re going to add a different type of power up. It won’t make the player “stronger” but it will enhance game play in another way; It will reward players by letting the extend their play. We’re going to be adding a “Repair” power up. By smart play, a player that’s able to maneuver to this power up will be able to repair one life of damage they have lost. What good is being able to take your enemies out in 1 hit, if there is only so far you can go in the game? This also let’s the creator become more crafty with their challenges. In Zelda Breath of the Wild when the player goes to pull the Master Sword out of the ground, it drains your health. Not because you failed to dodge something, but because that’s what it requires. It was a design choice to test the player in a different way than the norm.
First we’re going to make our game object for our repair power up. I took one of the other power ups into Gimp and changed the text at the bottom to say REPAIR.
I wanted to make this power up look different so I changed the design of the body, but kept the text a familiar color on white background. I named it Repair_Powerup. Don’t forget we need to add components of Box Collider 2D and Rigidbody 2D. Also set gravity to 0 on Rigidbody 2D. You’ll also want to adjust the collider to fit the size and shape of the game object. Then we’re going to attach the Powerup script to our new game object. Set the Powerup ID to 4 and attach a sound effect. We now have 5 power ups with IDs 0 through 4.
Before we start working on the code for the power up, let’s give it the ability to spawn in to the game. Click on the Spawn_Manager in the hierarchy and go into the inspector. In the Powerups list in the inspector add a new on to the list, just like we did for the ammo and drag our new repair power up into the empty field.
Now let’s go into the SpawnManager script and change something really easy. Since we increased our list we also want to increase the range of our random choice in powerups to spawn.
We increased the 4 to 5! Now the list items from 0 to 4 will spawn into our game. We simply need to add the repair functionality now. We’ll add a new method in our Player script first. Then we’ll call it from the Powerup script by adding a new case for the new power up.
Now that we’re in our Player we’ll make a method called HealthUp. What do we need to achieve in this method? We’ll need to update our _lives variable, fix the UI so it reflects the right amount of lives, and fix the damage on the player’s ship. Knowing these three goals lets make the method now!
Let’s go down the method and see what it does. First when the player collects the power up you’ll see that the _lives variable is updated by adding 1. This only happens if _lives are already less than 3. Meaning at 3, nothing will happen for that part. The next part will update the picture in the UI to reflect the player’s current lives by calling the UpdateLives method from the UIManager script. The last part checks the current amount of lives and updates the damage accordingly. Do you remember a long time ago when We first added the animated damage and we made a system of checks for current lives versus how much damage to show? We basically just copy and pasted here! That’s always fun when you remember something you already did or know how to do. I hope you enjoy learning and feeling comfortable in making changes you want to make.
Lastly we’ll need to make a new case in the Powerup script to call the method we just made. This part is so easy it’s almost fun; can you figure it out before looking?
You’ll see the newly added case 4. This calls the method we just made! Let’s think this through, when the player collides with our new object, case 4 is called. This starts the HealthUp method on the Player script. It checks if the player’s lives are less than three, and if it is gives the player another life. It then updates the lives image to reflect the change. The last part is that the animated damage is also checked and updated. Let’s see it in action!
You’ll see that the player is hit, the screen shakes, they lose a life, and there is some animated damage. When they collect the repair power up, the life is restored and the damage goes away.
Great work! That only took me like 3 tries to get that gif timed! Oh of course I mean great work on implementing a system at restores your players health. If you’ve been following up to this point you’ve really extended your abilities so keep it up. Have a great weekend and see you again!