Before we look into our powerup functionality. Let’s look at making them visually appealing so our player’s eyes will be drawn to them and therefore try to collect them!
First, we add a still image to our Hierarchy to create our game object that we will animate. Next, select Window > Animation > Animation or use Ctrl + 6 to open up the animation window. I find it best to be docked in my game window so I can access the project folder and also see my animations running in the scene view.
With our first powerup game object selected, our animation window will look like this.
Selecting create will open up our asset folder asking for a place to store our animation. It’s recommended to create an Animations folder for ease of use as projects get bigger. Now simply name your animation as desired.
Now we have a timeline. Select all of your sprites you want to be part of the animation and drag them in!
We now have a bunch of keyframes in our window. Clicking play allows us to preview our animation.
To me, this is cycling too fast. To fix it we can go back to our animation window and with all keys selected, simply drag the handle on the right out to our desired length of time.
Lets add some functionality to our powerup and test it in game. Create a powerup script, and similar to our enemy movement, lets use transform.Translate to move downwards by a specified speed. The difference with our powerup and enemy is that we don’t want our powerup to respawn, if you miss it, that’s it. A simple if statement to check when the powerup is below the screen can be used to then destroy the game object.
We now need our powerup to spawn. Let’s use our Spawn Manager.
Much like our enemy Coroutine, we want our powerups to spawn only when the player is active. So we once again use our while loop with our _stopSpawn bool that becomes true on the player’s death. Within this I’ve created a random position on the X axis, using Mathf.Round to limit the float to 1 decimal place. I find this just helps the game look slightly cleaner when there are a lot of elements on screen.
Next we use this random position to store our Vector3. Now we can Instantiate our powerup prefab.
Again to keep the Hierarchy clean, I’m assigning the parent of each powerup to be an empty game object I’m using as a container.
Now the last thing we need for a Coroutine and especially our while loop is our yield return type Waitforseconds to rerun our spawn method between every 3 to 7 seconds!
We haven’t yet added the triple shot aspect to our powerup functionality. But we can now see our animated powerups being spawned and travelling down the screen!