Adding Features: Health
Damage is done. We won the first round. Time to make some repairs.
To set up our object, we are going to follow the same process that we used to create our ammo powerup.
Find your sprite and drag it into the Hierarchy. Rename it. and add rigid body, collider and powerup script.
Once you’ve done that, drag your object into the prefabs folder, creating a prefab.
Next, select your spawn manager. Add an element to the powerup array and drag your health powerup into the array.
Back on the Health powerup object, drag your powerup audio file into the audio clip reference. Also, make sure that your powerup ID is the same as the element number in your powerup array from the previous step.
Dont forget to apply the overides. That will apply all of the changes you made to the prefab. Without it, your changes will only apply to the specific object you are working with.
Now for the easy part. Let’s build the logic!
First, in the spawn manager script. we need to update the range in our spawn powerup routine from (0, 4) to (0, 5).
Now let’s go to the player script and create the logic for life.
First, we need to check if our lives variable is at 3. If it is we do nothing.
If our lives variable is not at 3 then we want to add one to lives when we pick up the health pack. then we need to update the UI manager.
We also have a damage animation that runs when we get hurt. We need to reverse that when we pick up a health pack too. For that we will use if/else-if logic to check the lives and set the animations inactive.
We nest the if/else statements that control the animations inside the else portion of the statement that controls the lives. This ensures that the animations on change when the lives change.
Last but not least... We need to add another case for our health powerup in the Powerup script.
And then we test….