Creating a Shield Powerup — Part 1, The Pickup

In the last article we created some Animations for our previous Powerup collectibles. I think it’s time for another one!
The difference in this powerup will be that we’ll connect a visual shield to our Player to indicate that our Shield is activated!

But first we’ll create the Pickup to activate the shield and give it functionality!

Our eventual Shield… without any animation, yet.

Creating the Powerup collectible

As done previously, we’ll drag in a Single Shield powerup sprite into the Scene Hierarchy to create the base of our Shield Powerup object. Then add our Rigidbody 2D, Box Collider 2D, and our Powerup Script.

  • Remember to set the Rigidbody 2D Gravity Scale to 0.
  • Turn on Is Trigger on the Box Collider 2D, and edit the collider to fit the sprite.
  • Within the Powerup Script we’ll change the Powerup ID to be 2.

Then we’ll create the Animation (refer back to the previous article).

And then make your new Powerup a Prefab!

Shield Powerup collectible created, with all it’s components.

Organize the Animations

We now are beginning to have a lot of Animations and Animator Controllers. It’s best that we put them all into an Animation Folder such as this:

Organized!

Sometimes, when moving animations and controller, you may need to relink them to your GameObject. Do this if you find your animations not working after moving them into a folder.

Fix the Spawn Manager script

Now that we have our Shield Powerup Prefabbed, we’ll let the Spawn Manager know that it can use it.

On the Spawn Manager object, in the Spawn Manager script component, we’ll change the Size of the powerup array to 3, and drag the Shield collectible into the 3rd slot:

Spawn manager script updated

And inside the Script we’ll change our randomPowerup variable to include the Shield by incrementing it to 3 max value.

You could also use Random.Range(0, powerupsArray.Length) which would give you the same result. I will be changing my script in the future once I add more powerups.

Allowing for the new Powerup to be included.

Creating the Shield Logic

The Shield collectible spawns properly now, but when we interact with it, nothing happens! So let’s give it some logic.

Inside the Player script we’ll create a private bool _isShieldActive and set the default value to false. This will let our Player class know to utilize the Shield or not, initially not being used until the Powerup is collected.

Then we’ll create two public functions to activate/deactivate the shield.

Our two Shield functions.

Now we’ll jump into our Powerup Script to first activate the Shield when the powerup is collected:

Switch cases updated for our new Shield Powerup.

And now back into our Player script to finish off our logic.

We want the shield to be called during the DamagePlayer() function, so long as the player shield has collected the powerup and the shield is active. If so, then our shield will deactivate, and our Player will not lose a life.

Shield Logic

Our Shield should now be working! We don’t have a visual representation yet, but the logic is sound and can be checked in our Inspector when play-testing the game. Watch the lives on the right and notice how they do not decrease after collecting the Powerup!

Shield Logic in action!

In the next article we’ll give our Player an actual Shield so that we know when the shield is active or not!

--

--

--

Audio Engineer turned Unity Game Dev. Will be combining both my skillsets when appropriate, and will be documenting my Unity Dev growth in these Medium Articles

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

Recommended from Medium

A quick introduction to Django

Secrets of Software Development: Social Distancing Edition

Moving Platforms in Unity3D

UI Styling with Swift Enums

How to get two different shadow color horizontally in CSS?

Space Shooter Challenge: New Enemy Type

zkLend — two protocols for one purpose. Decentralization, security, speed

How To Set Up Dev-C++ For Your First Project

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
Frank Warman

Frank Warman

Audio Engineer turned Unity Game Dev. Will be combining both my skillsets when appropriate, and will be documenting my Unity Dev growth in these Medium Articles

More from Medium

2D Weapons Lab- 8-Direction Projectile

C — Static libraries and linking.

Tutorial on how to develop and deploy Neo N3 NEP-17 Token (Fungible) using C# from scratch

Installing and Setting up Kali Linux in Termux — Development with Android.