Health Pick Up
In this article we are going to go over adding in a health pick up to the game. This is going to built upon the Power Up system created before.
First, we create a Health pick up and I used Aseprite to make the 2D Sprite. As I move forward with his game I plan on slowly convert the game art to pixels.
Once we imported the Sprite asset we will add a box collider to it, so it will be able detect collisions. A Rigidbody is needed for collisions to work, well at least one of the objects needs a Rigidbody, along with both needing a collider to work.Since this is part of our Power Up system we will add the Power Up script and set the ID to 4.
Jumping into the Power Up script, we will add a new case (case 4), which matches the the Power ID we assigned in the Inspector. In this case, we will call a method from the Player called Increased Health.
In the Player script, lets add the Increase Health method (making it public so we can call it in the Power Up script) and this will handle the changing of lives, updating the engine damage, and the UI manager. Using an IF ELSE statement, if the lives are less the the max of 3 we will increase the lives by 1 and anything else (if lives go over 3) we will set lives back to 3 so it doesn’t exceed the max amount. If we move for 2 lives to 3 we will turn off the left engine damage and if lives are going from 1 life to 2, we turn off the right engine damage off. This keeps the damage animations consistent with the lives. After those checks, we call the UpdateLives method from the UI Manager and pass in the current lives amount.
Over in the Spawn Manager, we will need to update our random PowerUp range and increase it by 1. So the range will now include the PowerUp IDs 0, 1, 2, 3, and 4. When the Power Up is instantiate, the Health pick up in now part of the array and can be spawned. There is how you can add a help pick up that will increase the health, updated the visual damage and UI Manager.