Day 11: Coroutines With Unity

Gerald Clark
5 min readApr 13, 2021

Our game is looking nice. We have accomplished a lot so far, but we need to create some POWERUPS!!

We’re going to create a Triple Shot power up in this section. I’ll go through the steps on how to create the actual power up and then how to give it a cool down of 5 seconds. When you collect the power up, you’ll have 3 lasers for 5 seconds and then your laser goes back to 1.

FIRST lets create our actual triple shot prefab. We’re going to need to instantite this prefab when we need it, so it MUST be a prefab. Drag the laser prefab we made already into the hierarchy. Move it to the front of your ship like so:

Now duplicate it, and move it to the right wing tip. Duplicate that and move it to the left one without changing the y axis.

Pro tip: When you drag the laser to the right wing tip, check out the transform component. The X axis should be a positive number. If you duplicate that laser, and then change the X axis to that same number, but negative, it should move to the exact same spot on the left wing tip!

Now that we have our triple shot made does it make sense to instantiate the 3 lasers all at different times in our code? NOPE! Lets create an empty game object called “Triple_Shot” and drag those laser prefabs into it. Now we can prefab THAT game object so we only have to call 1 prefab when we instantiate it!

Prefab it and then delete it in the hierarchy. Lets take a look at a section of our code. In the player script we need to instantiate our lasers a little differently now that we have a Triple_Shot. We can do this with simple if statements.

As you can see, we have if(isTripeShotActive == true), and then an else statement for the regular laser. Obviously we need a variable of type “bool” called isTripleShotActive. Set it to false initially and give it a [Serializefield] attribute. We’re going to start the game in a minute and manually check the isTripleShotActive box on and off to simulate getting a power up. We’re also going to need a GameObject variable for our triple shot prefab to be instantiated.

Save the script and go into Unity. Assign the prefab to the prefab slot in your player script.

Run the application and check the isTripleShotActive box on and test out your triple shot power up! The lasers will still collide with the enemies. The lasers are still tagged as “Laser”, and that is why.

Now lets set up our power up so that it automatically sets our isTripleShotActive to true.

drag the following sprite into the hierarchy:

Resize it to something like 0.5 on the x, y, and z. Create a new script called PowerUp and attach it to the newly added triple shot sprite. Here is what your script will eventually look like:

So far we understand whats happening in the update function. Theres a line in the OnTriggerEnter2D() that says player.TripleShotActive();. Whats going on here? We’ve created a local Player reference in this function to get a reference to our Player game object and then getting the Player Component (aka the player script). We are communicating with our player script here. What are we communicating with you ask?

We have a private variable or isTripleShotActive. Its bad practice to make variables public, so we created a public method called TripleShotActive() that sets isTripleShotActive to true when called. So in our PowerUp Script we are calling this method when the power up collides with other.tag “Player”. Try out your game now! The box should automatically check itself when you collide with the power up.

Now that we have this working we need to set a cool down up for the triple shot to go away. Power ups are temporary, and should be limited! SO! The TripleShotPowerDownRoutine() is what is handling this. We are calling this to start in our TripleShotActive() method. This allows our triple shot to stay active for 5 seconds and then it switches back to false until the next power up is gathered! It literally says “while isTripleShotActive == true, run this for 5 seconds then set it to false”.

Nice work! Next time we will discuss our Speed Power up!

--

--

Gerald Clark

Father Game Developer Music Composer Sound Designer