It’s Coming together and Starting to Feel like a Game!

Games Woods
4 min readMay 14, 2021

We’ve started transitioning to our polished assets but we need to start adding some classic game elements live say a power-up system!? Specifically a triple shot power-up… Pew Pew Pew!

Luckily as we implement this we will start to recognize some familiar patterns such as creating empty objects, child objects, instantiating, colliders and rigid bodies as well as coroutines and let’s not forget to destroy those things when we don’t need them.

We begin by dragging a Laser prefab into our scene and duplicating it twice more. Then placing the lasers on the tip of each wing and at the top of the ship. Note the offset position in the Y-Axis.

We then create an empty object called Triple_Shot and drag those into it. We then drag it into our prefab folder in our project window and delete it from the hierarchy window.

Next we hop into our Player.cs script and create a SerializeField GameObject called _tripleShotPrefab and a private bool _tripleShotActive = false.

We then modify the FireLaser() method to check if the _tripleShotActive variable is true. If it is we instantiate the Triple_Shot Prefab. Otherwise we fire the regular old laser.

If we manually set the variable to “True” we can test to see that our Triple Shot is firing correctly. But what we really want is a PowerUp asset to give us this ability.

We drag in our PowerUp Asset from our art pack into our PreFab Folder and rename it Triple_Shot_PowerUp. We also create a new script called PowerUps and drag it onto our new asset Prefab.

In our new script we create a SerializeField GameObject variable called _tripleShotPrefab and a private float _powerUpSpeed = 3.0f

In our Update method we utilize similar code to how we made our enemies move down and get destroyed when they reach a certain position on the Y-Axis.

Making sure our new Prefabs have the correct Colliders and RigidBodies as our other objects (remember is Trigger and Gravity as well as editing our collider area ) We then proceed to add the logic for OnTriggerEnter2D(Collider2D other). We use CompareTag to ensure that the collision is happening with the “Player”. If so, we use script communication to invoke a method in our Player script that will switch the _tripleShotShotActive to equal true and destroy the PowerUp Object.

Jumping back to our Player script we need to create our TripleShotShotActive() method that the Powerup Script will call when it collides with the player. In it we set the value to true and then invoke our Coroutine to set a timer to return the value to false after 5 Seconds.

This is what our coroutine looks like:

So using a lot of the same principles and patterns that we’ve previously explored we start to see how we can use these over an over on different objects to perform vastly different yet similar behaviours.

Now it’s starting to feel like a GAME!

--

--