Adding another powerup — Shields

The first thing we are going to do is create our shield powerup just like we did our speed powerup.

We’ll go ahead and get this animated from the get-go rather than waiting until the end.

Then we add our Powerup script.

And before we forget, we set the PowerupID variable to 2 as that represents the shield option in our code.

Drag the Shield_Powerup game object from the hierarchy view to our Project view in the Prefabs/Powerups folder to create the prefab.

More coding

We know we’ll have to do some coding in multiple scripts, including the Spawn Manager, Powerup, and Player scripts.

Let’s start with the Player script.

We add a new Boolean variable to track the active status of our shields.

We add 2 methods to control the active status of our shields from outside this script and to implement a cooldown period.

Moving on to the Powerup script, we complete our switch statement code as shown above, calling the method we just created in the player script.

And on to the Spawn Manager script, we add a GameObject variable to store our Shield game object’s prefab.

Before we forget, let’s assign our new Shield_Powerup prefab to the SpawnManager script via the Unity inspector.

Back in Visual Studio on the SpawnManager script, we add the SpawnShieldRoutine() method as shown above.

Then we update our Start() method to fire off the StartCoroutine() method for SpawnShieldRoutine().

I don’t think the player should have constant shield powerups floating around, so I’ve increased the minimum spawn interval to 18 seconds and the maximum spawn interval to 45 seconds.

Aren’t we forgetting something?

We’ve got all of our code in place to spawn and activate our shield on the player.

Only, the player won’t see any difference and it has no effect on game logic!

We need to find a use for this shield first of all.

How about we design the shield mechanic as a kind of “save a life” concept.

If the player’s shield is active and he hit’s an enemy or otherwise would lose a life, the shield protects this life and then deactivates.

We need to modify our shield code in the player script.

First of all, let’s put a reminder to animate the player shield with a todo remark.

You can make commented remarks in C# using the “//” leading characters.

The compiler will ignore all that follows on the line.

Next, we need to get rid of the ShieldPowerDown() method altogether and remove calling it from our ShieldActive() method.

That leaves us with the above.

Moving on to the LoseLife() method of the player script, we add a simple if statement based on whether _shieldActive = true.

Equipping the shield

Let’s add some visual appeal to our shield powerup ability by actively changing the appearance of our player ship.

Thinking about how we’ll accomplish our task now, makes finding the steps easier.

I plan to use an animated game object for the PlayerShield and use code to overlay on top of our ship when the shield is active by parenting it to the Player game object.

To make sure the shield always renders on top of the ship, we’ll need to start by adding a new Sort layer.

I went ahead and added an overlay layer, as well as a UI layer for future use.

It never hurts to think ahead!

We’ll use the base sprite from our shield animation sprites to create a new game object and call it PlayerShield.

Once we’re done with the animation, we create a prefab as shown above.

In the Player script, we add a game object variable to store the PlayerShield prefab and another game object to hold the playerShield game object we’ll be instantiating.

In the Unity editor, we drag our PlayerShield prefab onto the Player game object’s Player(Script) component in the appropriate field.

That was a mouthful, but you get the idea.

A quick test with the shield powerup thrown in the hierarchy to immediately spawn allows me to do a quick and dirty test.

The shield animation looks far too small but otherwise is working.

I opted to go with 2.25 for the scale of x,y,z variables of the PlayerShield transform and edited the prefab directly.

We update our ShieldActive() method to instantiate the playerShieldPrefabGO game object with an assignment to _playerShieldGO.

This is so we can later destroy _playerShieldGO when the shield becomes disabled.

We update the LoseLife() method to Destroy _playerShieldGO when appropriate.

And that’s it! Save all of your scripts, save the project and run it.

Next time we’ll get started on the User Interface.




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

Recommended from Medium

Installing Prometheus in a AKS Kubernetes Cluster

CS373 Blog 5 Oct — 11 Oct

LAME(HTB Walkthrough)my 1st blog…

1Hive Monthly Recap — June 2021

Frequently asked questions about MicroStock+

Grading Students | HackerRank Problem | Solution

Game Dev in Progress… Day 6

4. Take profit separation, exchange, trade direction switch, dark theme, notifications, autosave

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
GameDev Dustin

GameDev Dustin

More from Medium

Application.Quit — Exiting the Application

Using Decals in Unity

Creating A Scene Template In Unity 2020.2 And Above

Introduction to Shaders