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);
}

--

--

--

Aspiring developer that’s self-learning Unity & C# to transition to a career with Unity. I got a passion for creating interactive experiences.

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

Recommended from Medium

How to use gif image in android

Customizing Switch using XML

Fragment Pop Bugs come with Support Library SDK 25.2.0

Android LiveData + Coroutines— The next level with map and switchMap extensions

Building your first Custom Chart in Android with Jetpack Compose

How do we manage store screenshots at BackMarket? (Part 1)

SurfaceView still Outperform Jetpack Compose in Rendering

Understanding Providers.

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
Tristan Engel

Tristan Engel

Aspiring developer that’s self-learning Unity & C# to transition to a career with Unity. I got a passion for creating interactive experiences.

More from Medium

Exploring the internals of a public library's network

BGP Remove Private AS

Developing A Game Engine with Perl: Part 5–32bit -> 64bit & Perl’s Storable

How to create a shared folder between VirtualBox Machine and your host machine