My Space Shooter is starting to feel like a real video game
Objective:
Implement a power up system that integrates with the SpawnManager
.
Backstory:
In my last article we covered moving from prototype objects to production game art. That left us off with a program that’s really starting to play like an actual video game. Now we want to add some features and in this write-up we’ll talk about adding a power-up system for a “Triple Shot” laser power-up.
The Artwork:
Our game art includes the sprites that we’ll use for our power-ups but to get started we need to make a GameObject
for the power-up itself, and then animate that. To do this is very simple in Unity.
We click and drag the first sprite for the Triple Shot and place it in the Hierarchy. I’ve renamed mine to “Triple_Shot_PowerUp” to make it clear what this is since later on I’ll be adding more power-ups. I’ve also adjusted the Sorting Layer
so we’re in the foreground layer.
Now that we have a GameObject
it’s easy to add the animation.
Animating Sprites:
With the “Triple_Shot_PowerUp” selected in the Hierarchy, we can open the “Animation” window by pressing Ctrl+6
(on Windows). When we do this, we’ll see the following screen:
This says “To begin animating Triple_Shot_PowerUp”, create an Animator and an Animation Clip” with a button to “Create”. Click on the “Create” button and this will open a file system explorer for your OS. I’ve made a folder for Animations where I can keep my assets organized, and I’ve named my Animation “Triple_Shot_Powerup_anim.anim” and saved it inside my “Animations” folder.
Now we’re ready to animate our power up sprite in the dope sheet.
To get started, we’ll click on the “record” button, select our sprites and drag them over to the dope sheet window that will add key frames for our animation…and that’s it. We have an animated power up!
All we need to do now to finish this off is to add a couple of components for our collision detection, namely: BoxCollider2D
and Rigidbody2D
. We need to check the checkbox for Is Trigger
on the BoxCollider2D
and we need to remove the Gravity Scale
on the Rigidbody2D
component. Now a quick click and drag the “Triple_Shot_PowerUp” from the Hierarchy window into our Prefabs folder in our Project window and we’ve finished making our power-up into a prefab. I’ve made a new folder inside of my Prefabs for “PowerUps” and we’re almost set to start working in our SpawnManager
to start spawning power-ups for the player to collect. But we need to add some behavior to our Triple Shot PowerUp first.
The behavior for the power-up is very simple. I’ve got a [SerializeField]
for a private _movementSpeed
to be able to control how fast the power-up moves down on the player’s screen. The actual movement is in the Update()
method and since that’s really all that’s going on there I went ahead and kept the code within that method. If I need to add more behavior then I’ll absolutely make additional methods to contain that logic. But for now, we just have the need to make the power-up move down the screen and once off the screen we Destroy(this.gameObject)
since the player missed the opportunity to collect the power-up.
We also have the collision detection that first sees if the object we’ve collided with is the player, all other objects we ignore. We get a reference to the Player
Component and “null check” it and call the ActivatePowerUp
method on the Player
class with the type of power-up we’re activating. Below is the updates to our Player.cs
behavior script:
We have two methods that control the player power-ups. We have the ActivatePowerUp()
method that takes which power-up we’re activating and an IEnumerator
that will handle the “cooldown” of how long the power-up should remain active. I’ve got a SerializeField
for priavte float _tripleShotCoolDown
so I can play test with how long the power-up should be active. As I add more power-ups to the game, the ActivatePowerUp
method will just have additional case
clauses and I’ll likely refactor the TripleShotCooldown()
to be a generalized method for all my power-ups.
Adding Power-ups to the SpawnManager:
The last thing we need to do is wire up our power-ups into the SpawnManager
so we can get them in the game. We get started with adding a few variables to our SpawnManager.cs
I’ve highlighted the code that’s been added in red boxes and we’ll get to the SpawnPowerUp()
below.
The SpawnPowerUp()
code is pretty straight forward and simple. I wanted to have a random interval of when a power-up can spawn. Right now my limits are within 3 to 7 seconds a new power-up will spawn in. I have a yield return
statement to wait for the random time to wait before I spawn in a _tripleShotPrefab
GameObject
at a random x position and at the top of the screen (7.0f
).
The results of this article are captured on the opening graphic animation. We’ve got a game that is spawning in new Triple Shot power-ups. You can play this game on your laptop or desktop here:
https://joseph-youngquist.github.io/space-shooter-pro/
I’d love to hear your thoughts 🧠 on where the game is at and headed 🚀!
Up Next:
I’ll be building up on this foundation with some additional power-ups for the game and then adding in the user interface.