Adding various Health Power Ups

This is a follow-up article, you can start here.

Adding Health

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.

Previous | Next



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gert Coppens

Gert Coppens

Software Engineer — Unity Game and Application Developer