2D Game Dev Journey — Day 6: Speed Boost Powerup
Welcome to 2D Game Dev Journey — Day 6: Speed Boost Powerup. For today, I will be explaining how I created the speed boost powerup and touch on the topic of code modularity while doing so.
Starting Point & Setup
Today I will be starting right where I left off in 2D Game Dev Journey — Day 5: Triple Shot Powerup.
To get started for today, the first thing I did was go ahead and get my speed powerup sprite animated and set up with the proper components for collisions, as well as spawning the speed powerup from the spawn manager.
I also attached to it the powerup script I used in 2D Game Dev Journey — Day 5: Triple Shot Powerup to make the triple shot powerup behavior work. If I run the game with the current code for the powerup script, only the triple shot lasers will be activated no matter which powerup we collect.
For the speed powerup, we obviously want to give our player more speed, not activate triple lasers. Even so, I still would like to use the powerup script I used for the triple shot behavior as the speed powerup and triple shot powerup will behave similarly. The only difference will be in what they provide the player. This is where some code modularity helps.
Implementing the Speed Powerup Logic
In a nutshell, modular programming is when you break up larger functions into their smaller individual functions, often to make these smaller functions interchangeable and usable to at least help accomplish a variety of other large functions. There are a variety of ways one can make their code more modular, and this is something you want to do in practice as it makes your code that much more versatile. Now there are a variety of ways one can accomplish making their code more modular. My personal favorites involve using abstract classes and delegates, but for today I won’t be using those. Today I will make use of enums and switch statements instead to implement my powerup types. One cool thing about using enums in Unity is that when exposed to the inspector, you can select from a dropdown menu all the types that enum has as shown below:
After changing up my code to handle speed powerup logic, which was similar to how I created the triple shot powerup logic, I got the result below:
Thanks for checking in and reading today’s article. Tomorrow, I will be extending the powerup code to include a shield powerup! As always, comments and critiques are welcome. If I have made any mistakes, please let me know so I can correct them for future readers. If you would like a more in-depth tutorial about anything I went over today, let me know and I will take the time to write something up.