Adding various Health Power Ups
This is a follow-up article, you can start here.
Let’s get straight at it, in the Player.cs Script, add the following method;
Alright this is pretty clear. If our Current Health is less than our Max. Health, we say that our new Current Health is equal to Current Health + ‘healthamount’, the integer variable we expose in the Method Name.
Now in the Item.cs Script we can simply execute this method passing through the amount we want to add. Like so;
Now when we assign a different ID to two different prefabs and the player picks them up we can specify how much Health we’d like to add.
Adding Health Over Time
To add Health Over Time to the Player we’ll make use of another Coroutine. First, we need an accessible method that is responsible for starting the coroutine. In this routine we make use of a step counter and declare that if the totaltime (equal to seconds) is less than zero and our Current Health is less than Max.Health → Add Health(healthamount), add a 1 second delay and subtract 1 from totaltime.
Now the only thing left to do is to assign the method to its respective ID in our Item.cs Script. We pass in 1 Health per second for the duration of 10 seconds.