GameDev : PowerUp system rework!!

Matteo Lo Piccolo
3 min readJun 4, 2021

--

Before we start, I made a simple change in my code here

Because make a “check” is always a good thing to do.

I use the keyword var, and automatically Visual Studio knows this is a reference for Player class.

Mouse over on var Keyword

Another thing we can do here is create a tag for Powerup.
Just to make everything easy to manage.

Now, back to our problem: we need to access the bool and change it when we collect the Powerup, because for now we can collect it, but nothing happen.

Objective : script communication and change the boolean when we collect Powerup.

I won’t talk about ownership yet but I will soon, because the properties make everything really good, clean and encapsulated with no problems.
For now we simply have to understand this: the communication between scripts is FUNDAMENTAL to create a Game, a project, what we want.
BUT, some variables, like this bool, we want to change their value ONLY within this class.

Without Property, the method we use to do this is create a public method to change its value.
With Pseudo Code we create the logic here

This is exactly what we want :
we collect Powerup, the Coroutine starts for TripleShot (5 seconds), then bool is set again to false.

We start with the easy part : boolean.
Two things : we change public to private, we don’t need this value in the Inspector, then change it to true inside Method.

Since it is now private, by convention has _ in front of its name.
To change it without break everything, use this shortcut :

We highlight the variable and click CTRL + R + R

Change it to _tripleShotIsActive and press Enter

With this method, we change this variable in EVERY POINT WE USE IT.
We have to watch out when we change variables without using this method, because if in some point in the script we use this variable and change it ONLY in one point, Unity and the Compiler will give us an error.
Here we have few lines of code, imagine houndred a thousand of line… We have to search every point to change this variable again, and this is not good!

Now we need a Coriutine to manage our Cooldown right?

Really easy to do : Wait 5 seconds and reset the bool to false. Nothing complex.

Now, we have our TripleShot and our Coroutine, but we need last step : we need to communicate with POWERUP.
When this Coroutine have to start?
When we collect Powerup.

Then in the Powerup script, in the TriggerEnter2D

Test it

My fireRate is now 0.15

This works perfectly!
TripleShot active for 5 seconds, then back to single.

Clean the code from Pseudo code

--

--

Matteo Lo Piccolo
Matteo Lo Piccolo

Written by Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!

No responses yet