Day 18 — Now we’re getting a game forming here

Connor Fullarton
4 min readApr 2, 2021

Hey and welcome!

So now we have the beginnings of a fancy game here, it’s time we start adding in some powerups that our player can take advantage of. The first powerup will be a triple shot powerup that will allow our player to fire off three lasers at once for a short period of time.

Best part of all this is that we’ve learned enough about coding to get this implemented right away. Pretty exciting right? So let’s go into the how part for this. Right off the bat you’ll want to be adding in your own triple shot object called TripleShotPowerup and create a behaviour script for it. I’ve named mine PowerupsBehaviour since we’ll have 2 more powerups that will behave in the same way when it comes to their movement and collision with the player so it makes sense to add reuse the same script.

You should be good and familiar with how we implement movement to our object using transform.Translate.

transform.Translate(Vector3.down * _speed * Time.deltaTime);

Hopefully you’ll recognise that Vector3.down is the same as new Vector(0, -1, 0). Then we’re using OnTriggerEnter2D(Collider2D other) and the GetComponent function to check for the collision with the player provided that we have a 2D rigidbody and box collider on our powerup. Then from that collision all we’re doing for the time being is destroying the object with Destroy() to give the illusion that it was collected by the player.

That’s all sorted so let’s get into something a bit newer here and create an empty object and call it something along the lines of TripleShot and make sure that it’s x, y and z position are all set to 0. Go ahead and set your player position to 0, 0, 0 as well for this part.

Next drag over one of our laser prefabs into the TripleShot object so that it’s the parent and then duplicate the laser twice using CTRL + D or by right clicking and copy and pasting it. Then position the lasers as follows or in anyway you prefer that lines up with the player model you’re using:

Slap that TripleShot object into our prefabs folder and we’ve got ourselves something ready to Instantiate when we collect the TripleShotPowerup object. Now the cool part about this and one of the beautiful things about prefabs is that we don’t have to worry about adding behaviour to our TripleShot object since all it’s going to be doing is bringing in three instances of the laser prefab at once which already have behaviour scripts assigned to them.

That means the collision with our enemies will work too! Now when figuring out where to put our code for instantiating our TripleShot it’s important to think about where it makes the most sense. In our case it makes the most sense to add it to our Player script as we already have code setup to handle laser firing and fire rate so let’s tweak what we have already in there a bit.

[SerializeField]
private GameObject _tripleShotPrefab;
private bool _collectedTripleShot = false;
private void FireLaser()
{
_nextFire = Time.time + _fireRate;
if (_collectedTripleShot == true)
{
Instantiate(_tripleShotPrefab, transform.position + new Vector3(-0.86f, 0.7f, 0), Quaternion.identity);
}
else
{
Instantiate(_laser, transform.position + new Vector3(0, 1f, 0), Quaternion.identity);
}
}

Now the main takeaway here is our _collectedTripleShot variable which is our first instance of using a bool in our code and a bool is only ever going to be a true or false value. In this case we’re starting it as false and then only instantiating our TripleShot prefab when the value is true.

Well we best get something in our code that assigns the value of true to it then!

public void EngageTripleShot()
{
_collectedTripleShot = true;
StartCoroutine(TripleShotTimer());
}
IEnumerator TripleShotTimer()
{
yield return new WaitForSeconds(5.0f);
_collectedTripleShot = false;
}

The important thing here is our EngageTripleShot() method here, that’s what we need to run in order to activate the triple shot. The rest of our code is just handling a Coroutine that deactivates our powerup 5 seconds after picking it up.

Now we need to think about where the best place to run the method is and that would be upon the collision of the powerup object with, luckily we’ve already got that in place and we’re getting an instance of the Player component to use the method too!

public void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Player _player = other.transform.GetComponent<Player>();
_player.EngageTripleShot();
Destroy(this.gameObject);
}
}

It’s brilliant how connected things can get when it all comes together!

I’ve rambled on long enough for this one, so in the next one we’ll sort out the spawning of our powerup and also a small dive into animation!

--

--

Connor Fullarton

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.