My 90 day journey becoming a Unity game developer: Day-19

Rhett Haynes
4 min readJun 15, 2021

--

Objective: Setting up the spawning and animation of the Speed Boost Powerup.

Speed Boost spawning and animating.

We want to associate the powerup we’re spawning with the powerupID. Right now we’re hard coding directly which powerup were accessing. If we have a lot of powerups to access, this would make our code very messy to read. So let’s use something that will allow us to store multiple objects into one area called an Array. An array allows us to store a fixed list of the powerups as elements that we can access using their element number. This element number is going to represent our powerupID numbers assigned to each powerup, and randomize each powerup to be spawned by that number.

We’re going to remove our game object variable tripleShotPowerupPrefab, and replace it with a same Type game object but with an array brackets [] assigned to the new variable name powerups. Comment out the Instantiate powerup inside the coroutine PowerupSpawnRoutine(). This will allow us to see the powerup array in the Inspector without any errors.

In the Unity Engine, click on the Spawn Manager and go to the spawn manager script component. There you should see the Powerups variable with a drop-down list, and an input box with zero in it. Double click on zero and add 3 for the number of powerups that will be used in the game. After you enter in 3, click on the drop-down arrow, and drag the powerup prefabs into each slot’s element number as follows:

0 = Triple Shot Powerup / 1 = Speed Powerup / 2 = Shield Powerup

Powerup array having the powerups assigned to each element by ID number.

Inside the PowerupSpawnRoutine() coroutine, we will create a variable name randomPowerup that will be used to randomly instantiate a powerupID number from the Spawn Manager game object. In the Instantiate method, use the powerups game object array and pass in the randomPowerup variable to select which powerup can be chosen to spawn.

Randomly spawns powerups using an array.

Make sure to limit the powerups to the 2 we created because the Unity Engine will crash if we try to spawn the third powerup which hasn’t been created or added to the array yet.

Randomly spawning Triple shot and Speed boost using the array data structure.

We have to now create the animation for the Speed Boost Powerup. Drag the Speed Boost Prefab into the Hierarchy. Make sure it highlighted and go over to either the animation tab to click on the create button. Or, go to Window->Animation->Animation to open the animation tab. Once we click on create and name our animation file and select the Animations folder to keep the animations in, click on the record button to start recording. Go over to our Speed Boost Powerup sprites in Assets, select all and drag them into the dope sheet. In the Scene View, press F to focus on the speed powerup, come down in the Animation window and press play to see the animation working. If everything looks alright, press the record button again to stop recording.

Testing the animation for the speed powerup.

Finally, make sure to highlight Speed Powerup Prefab, then come over to Overrides and Select All to save the new changes to the prefab. Delete the Speed Powerup Prefab from the Hierarchy and play the game to see the animation.

Speed Boost animating.

Powerup Shields are the next thing to create. Until then …

--

--

Rhett Haynes
Rhett Haynes

Written by Rhett Haynes

Learning to become a Unity game developer.

No responses yet