Space Shooter Redux: Day 2

Jeff Minnear
3 min readApr 16, 2021

--

Today, I’ve added 2 new features: a speed boost, and a shield that can take multiple hits. Let’s start with the speed boost.

We’re currently using the following pattern to handle changing the player’s position:

All we need to do to apply a speed boost is temporarily change the value of _speed. To make things simpler, I replaced the _speed variable with the following floats:

If you’re unfamiliar with the SerializeField attribute, it allows a variable to be changed in the Unity editor but still remain private. _baseSpeed gives us a convenient reference to the starting speed and allows us to easily return to it even if _currentSpeed has been modified.

Now we can update our move logic to check for boost:

Moving on to the shield, we’ve already got some cool artwork but the artist only made it in blue. To show different stages of damage, we want it to change color (from blue to purple to orange). We could try to track the artist down and ask for more versions, or jump into an image editor. But Unity actually provides a great way to do this with code.

Sprite renderers have a Color property. Adjusting it in the editor shows us what we can accomplish with it.

Here’s the code to modify the shield color:

Note the SerializeField attribute for _shieldColors. This lets us pick our colors in the editor instead of needing to hard code them. The last step is defining our DamageShield function.

Here’s the shield strength in action:

--

--

Jeff Minnear

Jeff is a software developer and musician. He’s quite fond of red curry.