Health refill powerup in Unity

Samarth Dhroov
2 min readJun 1, 2021

--

The exercise here demonstrates how to tackle health’s refill of the player when he collects the health powerup.

The mechanism of spawning powerups is already in place and is pretty flexible to add as many as needed.

So a few additions in the powerup script, spawn manager script, and a new method in the player script would make this work fine.

  • Add a prefab of health powerup with a box collider having “IsTrigger” enabled, rigidbody with 0 gravity, the powerup script & its’ needed id.
Unity
  • In the spawn manager script, expand the array to include the new prefab.
C#
  • In the player script, the need is to one, increase life by 1 when the current count is less than 3 and, two, repair the broken wings by disabling the damage animation.
  • Also, the total count shall always remain 3 and hence, the increment must check for it.
public void HealthRefill()
{
if(_lives < 3)
{
_lives++;


switch (_lives) {

case 2:
_rightDamage.SetActive(false);
_UIManager.updateImage(2);
break;

case 3:
_leftDamage.SetActive(false);
_UIManager.updateImage(3);
break;
}
}
else
{
_lives = 3;
}
}
  • Here is the final result :)

--

--

No responses yet