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

--

--

--

Software Engineer — Unity Game and Application Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Kava REST Server Guide

vim-templates — using template files for vim

knative Build Primitive

Why and how of centralized logging with ELK stack for Rasa - Part 2.

To attract top talent, companies need to adopt more balanced IP agreements

The evolution of modern apps: rewiring at the cellular level for optimum resiliency

Working at InfluxData: My Experience as a Remote Intern

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

More from Medium

New Input System with Unity Learn — Lesson 1

Connecting our Information and Working with Maps

Local control for Player

Creating MineField In Unity (Part 1)