Create a 2D Force Field with Dynamic Color Changes in Unity

Dennisse Pagán Dávila
Nerd For Tech
Published in
4 min readMay 21, 2021

Who doesn’t love force fields? They make you invulnerable, they look cool and Science Fiction has been hyping them up since 1931! Elements or power-ups that beget temporary invincibility have been present in video games for years — and as a principle of good game design, they are visually or audibly represented for the player.

Objective: In this article, you will learn how to make a forcefield that can absorb three hits before becoming inactive. Each hit will damage the force field which will be represented by changing the color of the shield as it loses power.

Setting up the Sprite Renderer

The Sprite Renderer contains the color property we need to access in our code. In this example, I placed my Force Field logic in the Player code because that is where I have a method that takes care of calculating the player damage.

Note: This code should ideally be placed within a method where you have access to the logic which substracsts life points or Health Points from your player.

  1. Create a global variable for the Sprite Renderer in order to make it accessible for our code. This will be a SerializeField so that we can assign the Sprite object directly in the Inspector.

Note: For more information on how the Force Field Sprite was animated click here. For more information on how to make a collectible power-up click here.

2. Create a global variable to determine the strength of the Force Field. This can be any number you see fit.

3.Create an if-statement that will be executed if the Force Field has strength left. The Force Field strength should be decreased if it’s greater or equal to 1. This should ideally be placed before you subtract from the player lives.

Note: The boolean you see within the if is not necessary to get the color change. However, in my code, the Force Field becomes active when a power-up is picked up and the bool variables is there to keep track of whether the power-up is enabled or not.

4. Within that if-statement, create a Switch Statement to switch the colors depending on how much strength the shield has left.

The color switch is done by searching the renderer using the variable we created for it earlier. Once we get the color, we can set the new one following the correct syntax as below.

Note: Case 0 contains code from my shield power-up logic which is not necessary for the color change. Here, I simply deactivated the visual representation of the sprite object so that the force field dissappears when it’s not active.

5. After closing the Switch Statement, we add a return statement which will make it so that the player is invulnerable if the force field has strength. The reason why this works is that my code is placed within a damagePlayer method where the player’s lives get subtracted. By terminating the damagePlayer method’s execution with a return before the player lives are subtracted, it prevents the player from losing lives. That is the reason why the lives subtraction is outside of this if-statement. Once the if condition stops being valid(when the force field runs out of strength), the damagePlayer method will keep executing as normal and the player will lose lives.

The Force Field strength is set back to full strength so that the next time the player picks up a shield power-up, the strength has been replenished.

The Result:

This was the second coding challenge from my apprenticeship at GameDevHQ, the coming articles will feature more of these!

--

--

Dennisse Pagán Dávila
Nerd For Tech

Software Engineer that specialize in Game Development. Currently looking for new opportunities. LinkedIn: https://rb.gy/xdu8nu