Unity Dev: Starting to Feel Like a Real Video Game!

Power-ups make the world a better place.

Even without adding anything else, we could call this a completed game.

However, it would get boring pretty quickly. Some of the features I plan to add are:

  • Collectable power-ups (triple shot, shields, speed booster, and others).
  • Visual effects like animated space ships, explosions and fire/smoke when the player takes damage.
  • Sounds (background music, laser fire, explosions).
  • A user interface that displays critical information to the player during the game, such as number of lives left, ammo available, power-up countdowns, and more.

With careful design, we can make the power-up system modular, meaning it can handle just about any type of power-up and we will not need a completely separate script for each type.

In this article, I will be focusing on the triple-shot power up.

So what will I need to consider? Well, here is a list:

  • We need to make sure the player script knows if we pick up a power-up and what kind it is.
  • We need a timer for the power-up so it does not last for ever.
  • Once the timer runs out we need to disable the power-up’s effect on the player.
  • We need to spawn the power-ups randomly during game play.

Let’s start with creating the prefab for the triple-shot power-up. I simply used three of the regular lasers and put them into an empty game object. I renamed this empty object to TripleShot and positioned each of the lasers to align with the player’s ship where the laser guns are located. Now I can instantiate the TripleShot prefab instead of the Laser prefab while triple shot is active. Neat huh?

The pseudocode for firing the triple shot instead of a regular laser looks like this:

We need a way to tell if triple shoot is active. I created a reference to the triple shot prefab game object just as I did with the original laser. I also created a boolean variable called _isTripleShotActive. (Remember to always add the reference to the prefab from the prefabs folder into the correct place in the inspector window!)

When the user collects the power-up we can set this boolean to TRUE and start a timer. Then we simply set it back to false when the timer expires. Here is the actual code to determine which laser to use:

Here is what this currently looks like when activated:

Nice! Now let’s add the actual power-up to the game and create a script for it’s behaviors. It will appear randomly at the top of the screen and fly down towards the bottom at a set speed just like the enemies. However, it will not reappear if the player does not collect it. It will simply vanish. Buh-bye.

Perfect! Here is the code for the power-up motion:

Lastly, we need to have the triple shot become active when the player collects (collides with and triggers) the power-up. Note that we only want just the player to have any affect on the power-up so we need to check the name tag of what ever triggers the collision event. Maybe we could also check for player lasers to hit it and destroy it? Of course it is possible, but for now let’s not make the player’s life too hard. :-)

Here is the code in the power-up script that runs when the collision is triggered:

This checks the tag of the game object that triggers the collision to see if it was the player. If it is, then the code gets a reference to the player script and stores it in a variable. It the check to make sure it worked (that the object exists) and if so, called the TurnOnTripleShot() function in the player script.

This function in the player script then sets the boolean to TRUE so that triple shot is active. It also starts a Coroutine to act as a cool down system for the triple shot power-up. After 5 seconds (or what ever amount of time we specify), the coroutine sets the boolean back to FALSE and effectively turns off triple shot. Here is the code (located in the player script) for that:

Let’ see it in action…

Firing single laser….grabs power-up…gets to use triple shot for 5 seconds…goes back to normal laser. YAY! With some animation and sound effects added, that will look and work great. Tomorrow’s article will cover animating sprites in Unity and we will animate the triple-shot power-up as a demonstration.

See you there!

--

--

--

Math and science teacher / self-taught programmer and Unity game developer working hard to get into the game development industry fulltime.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Blog Series: Understanding Microservices using AbhiShop App

Medium and Slack moved away from passwords and here’s a reason why you should too!

Postgres: extract integer prefix

Design Pattern : Strategy Pattern

Linux awk/gawk programming with Practices

How To Break Software? — Tentamen Software Testing Blog

Amazon EFS: Has It Become Amazon ECS’s Best Friend?

Create an Azure Databricks workspace using Bicep Language

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
Michael Hatfield

Michael Hatfield

Math and science teacher / self-taught programmer and Unity game developer working hard to get into the game development industry fulltime.

More from Medium

Ease Of Building UI Elements In Unity

A Guild to Beautiful Game

Porting VR Games to Oculus Quest

Damage VFX using Animated Sprites in Unity