Geek Culture
Published in

Geek Culture

Creating a Shield Damage Feature Using the Alpha Color of a Sprite in Unity — Part 01 (Setting the RGBA values)!

Objective: To build a Shield Damage feature within our 2D Galaxy Shooter using the alpha Color of the sprite image in Unity!

Before we dive into this, I am going to be showing you 2 different approaches to adjusting or manipulating the alpha Color of a sprite image. This first article is going to be dealing with setting the Color’s RGBA values. The second is going to cover how to change the alpha attribute ‘color.a’ as this value can’t be changed directly.

As we approach the end of our 2D Galaxy Shooter game, we have a few new features to implement before we can call it a day. This time, we are going to be creating a Shield Damage system where the player is able to take 3 hits while the shield is activated before the shield is destroyed. Currently we have it only set up to take 1 hit and it isn’t quite working as the enemies now have 2 lasers they are able to shoot and if they both hit the player then it is taking 1 shield and 1 player life from them. Let’s give the player a chance!

Let’s Add Some Code!

As our ‘Player’ script already has the Damage() method built into it, let’s continue working in here:

Firstly we need to create a variable that is going to store how many hits our shield is allowed to have.

We are also going to need the SpriteRenderer component from the shield game object so don’t forget to GetComponent this and lastly, let’s make sure to null check that we are successfully grabbing this component from the shield game object. As my shield game object is deactivated at run time I can’t use the GameObject.Find() method for this as it only searches for active game objects in the scene. However, Transform.Find(“Shield”) will successfully find this game object without it needing to be active, and then as the shield is a child of the player game object (parent), we are able to GetComponentInChildren<>() for the SpriteRenderer!

Lastly, within our Damage() method, let’s check to see if the player’s shields are activated first. If they are, let’s run the ShieldDamage() method instead of taking a player’s life.

Now let’s add some more code to our existing ShieldBoostActive() method that is going to set our _shieldHits to 3 when we collect the powerup and also let’s set our player’s shield Color values to white (1f, 1f, 1f, 1f). This preserves the sprite images colours as this is normally how it would show as seen below:

let’s now create a ShieldBoostDeactivated() method that gets called when the shields charges are 0. This is going to simply switch the _isShieldBoostEnabled to false and switch off the shield game object.

Lastly, we need to create our ShieldDamage() method that is going to run when the shield is activated and a collision occurs:

This method ONLY gets run, when _isShieldBoostEnabled is set to true from with the Damage() method as shown in the first image. In saying that, our shields charges will never be 3 in this method, as we set the charges to 3 within the ShieldBoostActive() method and also set the alpha to be 1 (its maximum). So we don’t need an if statement for the _shieldHits being 3 as it will always be 2 or less when the method is run.

When our _shieldHits are 2, let’s set the sprites alpha to be 2/3 of its maximum value (0.66f). Then, when the _shieldHits are 1, set it to 1/3 of its max value (0.33f). Lastly, when our _shieldHits are less than 1 or 0, let’s run the ShieldBoostDeactivated() method to switch the shield off and now the player can start taking damage again and removing lives.

Note: The alpha’s range is between 0 (being fully transparent) and 1 (opaque).

Next Up: Let’s look at changing the shields alpha colours again but a slightly different way using ‘color.a’

--

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Recommended from Medium

Modify naming convention for Windows Virtual Desktop session hosts in ARM template file

Simple Explanations of Arithmetic Circuits and Zero-Knowledge Proofs

The Twelve* Glorious Flowers and the Dao of Programming.

Day 46 — Impovements: homing missile

10 free stock photos you would actually use (Monday 23rd 08PM edition)

How to Calculate the Similarity Between Image Hashes in Python

Community Update S1E1

A journey on the WASM’s land

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
Chris Hilton

Chris Hilton

Passionate Unity Game Developer

More from Medium

Framing Transposer Virtual Camera Explained in Unity’s Cinemachine

2D Mobile: Setting Up The Player

Making a RTS game #49: Optimisation tips & tricks (Unity/C#)

Tiling a Procedurally Generated Unity TileMap