Nerd For Tech
Published in

Nerd For Tech

Cooldowns and cleanup

Now we will try and figure out how to implement a cooldown system into our fire rate. This will enable us to put some sort of balance control into our game, as it can prevent people from hitting space as much as they possibly can to have an almost straight line of lasers.

As we can see from above, it looks a little unrealistic to be able to shoot at this rate. Therefore, we have to figure out a way to get a cooldown system in place. What we will have to look up a line of code of time.time.

What time.time does is keep track of how long the game has been running. If it’s been up for just 30 seconds, it has it set to 30, if it has been up for 10min, than it is set to 600. Now what we can do with this is go back into our player script and add a couple things to our code so that we are checking to make sure that we are able to fire again because enough time has passed by.

As we can see from above, no matter how fast I spam the fire key, our lasers will come out only when the proper amount of time has passed. As for the lines of code we needed to use, we just had to create a couple private floats for fire rate and can fire. These are for how fast we are wanting the be able to fire, and we can adjust it to whatever we want, and for can fire, we are setting the minimum bar so that when we decide we want to fire, we have the time period we can deduct from the time to determine if the statement is true.

Cleanup time

Now that we’ve finished up working on our laser, we should get around to cleaning up our scripts.

First lets take a look at our laser script:

Before cleanup

As we don’t need the pseudo code for laser destruction point, or the void start part of our script, we can just delete it:

After cleanup

Now to head over to our player script, where we have our laser fire mechanism set up:

Before cleanup

As we can see from here, we got just a little bit of pseudo code to delete. Also, we can move our fire directives down to a new void, as we did with movement, and keep our void update section cleaner looking. As to how we can make it look, there are a couple methods.
1) We can keep the if statement in the update and have a our FireLaser void be called when it is true.

Option 1

2) We can just move the whole statement down below, and just have it so the FireLaser is the only line of code within the void update section.

Option 2

Now that we have that all cleaned up, we can now look into creating some enemies for us to shoot at and how to introduce some physics into our game world.

--

--

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