Adding Health Collectable in Unity
Player can collect lives
Objective: create a collectable that add one live to the player
To create the collectable I dropped the respective sprite in the scene and renamed the game object to “Health Collectable” and made it prefab.
Then I added the collider and rigidbody component to interact with the player.
After that I added the powerup script to the health collectable prefab and assigned the ID 4 (I used the powerup script because this collectable has the same behaviour than a powerup).
In the spawn manager object I added the health collectable prefab to the powerup array.
In the powerup script I added a new case for the ID 4 to call the function to add a live to the player.
In the player script I created a public function (the same function that we use in the powerup script) to add one live to the player.
Next in the player script I created a function called DrawEngines to check the current lives of the player and show/hide the engine damage according to the lives. This function is called each time the user loses or earns a life.
And good to go!!! The player recovers a life when take the new health collectible.