Animating Sprites in Unity

Austin Hall
4 min readOct 3, 2022

--

Creating the Triple Shot Powerup

Now that we have our triple shot laser working correctly, we need a way to turn it on and off. Let’s start by Creating the the object for collection. For this we’ll use the first Sprite in the triple shot sprite folder.

Activating Triple Shot Powerup

In your assets folder, expand the Sprites folder, then the powerups folder, and finally the triple shot folder. Click and drag the first object into the hierarchy tab and rename it triple shot powerup.

Creating the Triple Shot Powerup Prefab

After we’ve created the object, we need to make it a prefab. To do that, we drag the object from the hierarchy tab, into the prefabs folder. Just like with the triple shot laser, we need to add a couple of components to the powerup for it to function properly. After dragging the powerup into the prefabs folder the object in the hierarchy tab should turn blue. There should also be an object in the prefabs folder. I mn bmf you don’t have an object in the hierarchy, drag the prefab into the hierarchy.

Collider2D and Rigidbody2D Components Added to Triple Shot Powerup Prefab

Working from the hierarchy tab and the inspector tab, select the powerup object. Add the rigid body 2D and collider 2D components to the powerup. Make sure that “is Trigger” is checked on the collider and set the Gravity scale on the rigid body to zero. Select the overrides drop down and apply all.

Triple Shot Powerup Prefab in Spawn Manager

We’ll need to create a game object variable in the spawn manager, similar to the enemy. We will also need a powerup script. Instantiate the powerup in the update method as follows…

Powerup Script, Moves Powerup Down On Spawn & Destroys at Bottom Boundary

In the player script, we’ll create a coroutine for activating and deactivating the power up. We’ll use the yield event ‘WaitForSeconds()’ as our timer

Player Script Method for Triple Shot and Coroutine For Cooldown

In the powerup script, we’ll use our on trigger enter method, some nested if statements and null checking to check for collision with the player. If the Object collides with the player then we activate the powerup method from the player script.

Powerup Script, Checking For Player Collision (Collecting powerup)

In the spawn manager, we’ll use another coroutine to spawn the power up at random positions along the x-axis. the yield event lets the powerup spawn between 3 and 7 seconds ( 3 is inclusive, 8 is exclusive).

Spawn Manager Powerup Coroutine
Spawn Manager Running Coroutines at Start

We can animate the sprites to add some flair to our game. To do that, first make sure your powerup object prefab is in the hierarchy tab. Select it, go to Window, Animation, Animation. This will open up an animation window. drag that window onto the console and it will create a tab.

In the Animator window you will see a message that reads “To begin animating Triple_Shot_Powerup, create an Animator and an Animation clip.”

Select the create button. A pop up box will appear. We want to create a new folder Labeled Animations. Then go inside that folder and name your animation Triple_Shot_Powerup_anim.

in your animator tab, you will see a record button in the top left. Select that button to enter record mode. While in record mode, open up the triple shot powerup inside the sprites folder. click on the first one to highlight it, hold shift and click on the last one to select them all and drag them directly into the animation. You can now stop recording, it will automatically apply it to the object.

Animation dope sheet

If you open up your animations folder in your project tab, you will see that there is a triple shot animation. There will also be a triple shot controller. This is what actually controls the logic for the animation.

Unity, by default, will add the animator component with the controller attached to the object.

--

--

No responses yet