How to create Repair Powerup

Chance the get back into the game after taking major damage

Today we will create an repair power up. Previously we already created an power up system. We will expand on this.

GameObject Repair Powerup

1. Create gameobject
2. Add animation (optional)
3. Add AudioSource
4. Add SFX to play when picked up
5. Add Component: Rigidbody 2D
6. Add Component:Collider 2D
7. Check Is Trigger
8. Gravity Scale: 0
9. Adjust Collider
10. Make it a prefab

Add it to the spawn_manager so it will be spawned.

Script Repair Powerup

1. Add the powerup script that plays the sound when picked up and tells the player script which powerup was picked up.

Our Player script has all powerup effect logic.
2. Create public method increase health
3. Extract method player visuals (before it was under public damage method)
4. Include call to update health UI
5. Change health visual switch statement to update visuals not only to support decreasing health but also increasing health.

public void Damage()
{
AudioSource.PlayClipAtPoint(_playerDamaged, Camera.main.transform.position);

//use shakebehaviour script method
_camera.CameraShake();

if (_isShieldActive == true)
{
//deactivate shield gameobject
_shield.SetActive(false);
_isShieldActive = false;
//exit function with return
return;
}
else
{
_health--;
}
PlayerHealthVisual();
}

private void Repair()
{
if (_health < 3)
{
_health++;
PlayerHealthVisual();
}
}
private void PlayerHealthVisual()
{
//hurt animatoins
switch (_health)
{
case 3:
_leftEngine.SetActive(false);
_rightEngine.SetActive(false);
break;
case 2:
_leftEngine.SetActive(true);
_rightEngine.SetActive(false);
break;
case 1:
_rightEngine.SetActive(true);
break;
case 0:
Instantiate(ExplosionPrefab, transform.position, Quaternion.identity);
_spawnManager.OnPlayerDeath();
Destroy(gameObject);
break;
//same as case 0: just in case a player is damaged twice in a frame
case -1:
Instantiate(ExplosionPrefab, transform.position, Quaternion.identity);
_spawnManager.OnPlayerDeath();
Destroy(gameObject);
break;
default:
Debug.Log("Default value in switch statement in Player script for health");
break;
}
//UI player health update
_uIManager.UpdateUIHealth(_health);
}