Adventures in GameDev with GameDevHQ! Day 22: Determining how long powerup effects should last.

Esteban Ibarra
5 min readApr 3, 2021

--

So we just animated our Tripleshot powerup container and put everything we need for it to be in the game; BoxCollider2D, Rigidbody2D and of course the script. How do we get it interacting with the player to give it a tripleshot for say, 5 seconds?

Taking the problem step by step, the first issue is right there. 5 seconds. Let’s put that into a variable in the Players script.

The reason we do this is maybe we’ll be using this same script on another powerup item and the designers will want another rule for how much the powerup will spawn. This is a handy variable to just keep out there just in case.

We’ll also need another variable to tell us whether or not TripleShot is activated or not:

We’re making the variable available in the editor just so we can turn it on or off ourselves if we need to, we’ll remove the [SerializeField] when were completely done implementing the powerup.

There’s a function we have now called FireLaser that’s called when the player is hitting space and has permission to fire. We’ll use our _isTripleShotActive variable to choose whether the tripleshot is instantiated or the regular laser is.

In PseudoCode:

Fire Laser

If TripleShot is activated, fire tripleshot

If not, fire regular laser.

This is how I translated that into code:

Pretty straightforward, let’s see how it works and we’ll use the serializeField to turn the _isTripleShotActive on and off.

Everything is working perfectly! When _isTripleShotActive is set to true, it Instantiates the tripleshot gameobject, else it instantiates the regular laser object.

Great! So what next? We need the powerup to tell the player to activate the tripleshot when the player object enters the powerups boxcollider. Since the powerups BoxCollider2D is a trigger, we’ll use an OnTriggerEnter2D event inside of the powerups script to check if the players collided with it.

Looks like we’re getting a little ahead of ourselves! Before we do that, remember that the powerup script itself can’t do anything! It can only access a script inside of the Player so since we want our TripleShot to activate when the Powerup touches the player, we need to set the _isTripleShotActive variable to true from the Players script itself, so let’s create a public function that can be accessed by the powerup that sets the _isTripleShotActive boolean to true.

Awesome! now we have something the Powerup can access and activate!

Remember when we need to access a method in another script, we use the GetComponent method, but we can only use that through an objects transform which is the only publically available way we can access another GameObject.

So let’s take a look at our Powerups OnTriggerEnter2D code:

It checks to see if it runs into the player, if it does, it creates a temporary home for our Player gameObject, and takes the script out of it using the getComponent function.

The if statement is a best practice. Sometimes there can be a situation where the player doesn’t exist and you’ll get a nasty error. Instead, we head it off at the pass and if it doesn’t exist, it doesn’t run the code we need from it. That said, it should exist and it accesses the Activate Tripleshot function.

Let’s give it a go!

Well, it’s definitely working the way we want it to. The powerup touches the player and the player now shoots tripleshots…forever. We don’t want that. We only want it to be a limited time. Now what was that function that helps us with time? Oh yeah! Coroutines! and if the tripleshot is activated by setting it to true, then logic dictates we only need to set it to false after 5 seconds! And since we’ve already programmed coroutines before, this is gonna be easy!

First, we’ll need to create an IEnumerator for the function we’re going to call. I called it FinishTripleShot. Pretty descriptive and we’ll know what this does if and when we return to this code 5 years from now.

Just like the Sith work in twos, So does a Coroutine. IEnumerator is StartCoroutines sith apprentice, and yield is the apprentice to IEnumerator

so as you recall from before, we delay time using yield like this:

So let’s wait 5 seconds, and the set _isTripleShotActive to false to turn it off!

_tripleShotTimer we set as a variable at the beginning for 5 seconds because maybe we’ll want to change it later. Anyhow, let’s see if it works!

It works and it works beautifully! So now you know how to set a time limit to a powerup, or anything else you want to set a time limit to, essentially.

We’ll get into modular powerup systems next!

--

--

Esteban Ibarra
Esteban Ibarra

Written by Esteban Ibarra

cartoonist, game artist, and wanabe gamedev.

No responses yet