Shields Up!

Jordan Evans
Nerd For Tech
Published in
3 min readMay 19, 2021

Now that we have triple shot and speed boost going for us, it’s time we take a look at getting our shields going. This one we will have to not only make a sprite that works for us, but also give our player a visual enhancement to show that we have an active shield.
For our basics though, we will start with the same process as our speed boost and place our sprite into the hierarchy then attach all the required components to our sprite. Those being the rigidbody, box collider (or circle if you prefer), powerup script and animation.

With a sprite made, it is time to code our shields so that we don’t take damage when we receive a power up. To start, we need to create a Shield bool, and then make adjustments to our damage portion of the player so that if we have shields active, we don’t take any player damage and then it ends the power up so we do not have an infinite shield.

Next, we need to switch over to our powerups script and get our shields effect connected to our newly made sprite, and link to the player script so that when we collect the sprite, we activate our shields. Once this is all done, we can run our game quick and see if the shields prevent our player from losing a life.

As we can see, once the player collects the shield sprite, we do not lose a life upon hitting an enemy. As well, our player continues to lose life after losing the shield from the first hit.
Now, what is a shield without having some actual visuals of our shield being active. To do so, we will drag over a shield sprite into our hierarchy and place it as a child to our player.

Now with our shield placed over our player, we now have to create a code so that when our shield becomes active, we will have our blue field appear around our player. To do this, we have to use the setactive code. With this line of code, we can choose to activate a specific gameobject or deactivate that object.

What we will add is the setactive code as true within our shieldactive and then within our damage void, we will set the shields to false once we take damage. Now that we have this built into our script, we can test it in our game.

Now that we have our shields powerup set, we can move on to building up some UI for our game to allow us to have a better visual of the status of our game as we play it.

--

--