Creating a Health powerup

Todays challenge is a little more complex, but not that difficult. We’ll create the PowerUp the same way we did yesterday, but give it an ID of 4 and add a new slot to the Spawn manager. And since we now have a more dynamic way of keeping track of the PowerUps so we don’t need to touch the code anymore! Just plug it in and go!

Now we just need to tell the powerup script what to do when it encounters the player and it happens to be this powerup:

Add a new case, Case 4: and we’ll create a public function called AddHealth. Let’s head over to the player script and do it!

A little bit more code here, and some regarding an animated special effect. Creating this animation was pretty simple, just hit the record button, go to frame 30 and scale the ship up and alpha down, DONE!.

This animation is looping, but we’ve turned it off in the animation object.

In action, you can tell something’s happened and when you pick up the power up, see that ghost effect scale up, and then see that one of your thrusters isn’t smoking anymore, you’ll begin to associate that animation with getting health back!

So we turn on the animation GameObject with SetActive, it has its own Animator component, so we take control of it and tell it to play “HealthUp”, the name of the animation where it grows bigger and disappears. then it Invokes a function TurnOffHealthUp which is just a cheap way of turning off the animation when its done after half a second.

Here’s the meat of the function. We add one more life to _playerLives, then just to be safe, we check to see if its 3 or over, if it goes over, we just push it down to 3 again assuring that our program won’t break.

We then call the UI Manager and tell it to update the ship graphic:

As you remember, the sprite array coincides with how many lives you have, so when you tell it to update and you have say, 1 ship, it will jump up to the 2nd position of the array that holds 2 ships and put that up on the screen! Pretty ingenious!

Finally we call DamageShip(), but why? we don’t want to damage our ship! Well, the function’s name is a bit deceptive. Yes, it shows damage to the ship, but now we’re going in reverse! We’re no longer a laser hitting the player and going 2 to 3, we’re a powerup and we’re going to reverse it back to 2!

So let’s say we were at 1, just one more hit would have blown us up. Both Thrusters are now smoking. But since we added a life, _playerLives is now at 2, and in case 2, the left thruster is still smoking, but the right thruster isn't anymore! if we get another powerup, then both power thrusters will be working normally!

wait for it…

Yes! As you can see, the Health PowerUp gives the player one more health and another chance at life! Next we’ll create a new secondary fire powerup!

cartoonist, game artist, and wanabe gamedev.