Nerd For Tech
Published in

Nerd For Tech

Creating, Animating and Implementing a Speed Boost Powerup

One down and two more to go when it comes to making a trio of powerups for my Unity2D game. I start with a simple circle shape, and then use a type on a curve feature in Corel Draw. Typing on curves is a very powerful tool when it comes to font design. I then add another font layer for my boost text.

I draw a simple whale with the pen tool, and add emphasis to the tail flipper. This is a speed boost powerup, and I want the player to get an idea of what they are picking up before they collect it. First, I convert my font to an object, breaking it apart from the curve so I can shrink it down and make some overlap with the circle behind it. I then cut, paste and offset a copy of the text. I change the color to the same dark blue as the background, and drop it behind the light text in layer order. Quick and easy text drop shadow accomplished! I add a fountain fill to the center circle, then copy and paste a few more circles behind the original one, and make them progressively larger in size. The colors for those new circles, as well as everything else in the image, are chosen from my already existing palette to fit the theme.

I make the whale icon the same color as my hero, and offset the text drop shadow a little more for some additional depth. Powerup icon complete!

Now to get into a little animation, I make duplicates of the whale shape and move/rotate them around to simulate some flipper motion. I don’t want the body to go crazy like seen in the images below, so I make a main body shape, followed by individual flipper shapes for the tail movement.

I can now stitch the tails to copies of the body to make one object, and start laying them out on a sheet for sprite animation.

After some tweaking to get my whale shapes where I want them, I do another animation on the boost font. The intention here is to have a subtle swimming effect as the powerup moves. I’m all set to jump back into Unity!

First I add my sprites to the appropriate folder for importing. Then I set the import settings to be ‘2D and UI’, and apply changes.

My powerup of course needs a few more things, such as setting the Tag to Powerup, and the layer to foreground.

In a previous article, I introduced the powerup ID variable in my powerups script, to house my different powerups as their number grows. The powerups and their respective numerical counterparts are referenced in the code comments.

Now when I drag the powerup script on to my speed boost powerup game object, I can assign it’s powerup ID a value of one in the inspector. Collider2D and Rigidbody2D components are also added.

One of the last steps is making a new animation via drag and drop, with the newly imported sprite assets.

After tweaking some keyframes on the timeline for a more desired animation, my powerup is ready to be made into a prefab. At this point I have also deleted the dummy prefab I was prototyping with.

In my last article, I went over converting a series of if else statements into a switch statement. The below activity is happening in the OnTriggerEnter2D method on my powerup script, which is what will be activating the speed boost on the player script when the powerup is collected.

OnTriggerEnter2D Method. Powerup Script.

My player script needs a bool to check if my speed boost is active of not.

Player Script

I want to start modifying the value of my original speed variable, so I no longer need that field to be serialized. I then create two more speed variables. The default speed variable essentially replaces and holds the value of the original speed variable. As expected, the flipper boost speed variable will hold the value for how fast the powerup makes the player move. I want to be able to control my speed values in the inspector, so I serialize both fields.

New player speed variables

All I need is a simple method that can check and set the bool value when the powerup is collected. Here I am checking to see if the speed boost is active, and if it is not (is equal to false), my speed is set too the default speed. If the speed boost is active, my speed is set to the boosted speed.

Simple powerup speed mechanic

Being the speed variable has a direct relationship to player movement, I need to place this accordingly in my Player Movement method. Scripts execute top to bottom, so it’s very important that I call my speed boost method before my player movement (Vector3 _direction). The speed value being used in the movement statement needs to be updated before that part of the script executes, or I would have to work out another way to set the speed back to normal after the powerup wears off.

Finally, I have a public void for the speed boost activation method, which is what the powerup is triggering upon collection. That method starts a coroutine, which sets the bool to true, meaning our speed boost is now active. After waiting for five seconds, the bool is set back to false, and the coroutine ends until another speed powerup is collected!

The next step is moving to my Spawn Manager script to work in my new speed boost powerup. I was previously hard coding in my spawn manager to only drop triple shot powerups. I need it to randomly drop any powerup. I delete my game object variable that held my triple shot prefab, and add a new powerups variable, which will store an array [] of game objects.

Spawn Manager script

Now in the inspector on my spawn manager, I have powerups references in a drop down list called elements. I can drag and drop the appropriate powerup prefab into it’s correct element assignment.

Missing prefab assignment
Assigned speed boost poewrup

I want to be able to adjust my spawn rates in the inspector, so I add a few variables and serialize the fields. I will use a random range for timing the spawning of powerups, and that can be controlled through the min and max variables. If I try to serialize these fields in a coroutine, Unity will yell at me saying, “Hey! You can’t do that!”. So this is my workaround for not hard coding those values into the coroutine, and giving a designer some extra values too adjust.

I need to initialize the min and max variables in void start, followed by starting both coroutines spawning enemies and powerups.

In the powerup spawn routine, I can now change the old instantiate(triple shot prefab), to instantiate powerups, followed by the array brackets [] to reflect the powerup array assignments in the inspector. If I placed a zero in the array brackets, only the triple shot would spawn. The same can be said about the new speed boost powerup if I assigned a value of one in the array. To make this random, I introduce a local int variable for random powerups. This takes an assignment of a Random.Range that is exclusive concerning the last digit. What that means is the value of three will never get called. If it does, I will get a Debug.LogError message telling me that a powerup was collected outside of the assigned parameters.

I now have some randomly spawning powerups, but for the sake of a demo, I dropped a few speed powerups into my scene view to show the new powerup at work. Sometimes a random drop is very random, and it takes a while before the speed boost actually drops! For my next article, I will finalize the shield powerup assets, so I hope to see you there. Thanks for reading!

--

--

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