Animating Sprites in Unity
Making a looping sprite animation is easy in Unity. All that you need to do is create the sprite gameobject, give it an Animator component, open the Animation window and then drag in the sprite sequence and it is done. To show how to do this, we will make a Triple Shot Power Up for the 2D space shooter in previous articles.
First, create the sprite gameobject, name it “Triple Shot PowerUp”, and drag in the first sprite in the sequence.
The easiest way to create the Animation clip, Animation Controller, and Animator component for the Power Up is to select the gameobject and then switch to the Animation window and click the Create button. That will open a window where you can name and choose where the Animation clip and Animation Controller will be saved. Name the clip “TripleShot_anim” and then drag the sprite sequence into the Timeline.
Now you can turn on Preview and hit the Play button to see how it looks. If the animation is too fast or slow, you can either adjust the Sample Rate or change the animation’s length in the Timeline.
As you can see in the below image, the Animation Controller was named after the gameobject. It is always good to keep organized and have the animation clip and Animation Controller in an Animations folder; with the animation working, it’s time to set up the PowerUp.
Change all of the Scale values to 0.325 so that the gameobject is closer to the Player’s size. Next, add a Rigidbody 2D setting the Gravity Scale to 0 and a Circle Collider 2D setting Is Trigger to enabled. Then create a script named “PowerUp” and attach it to the Triple Shot PowerUp gameobject and make it a Prefab.
Now to create the Triple Laser, which will be fired by the Player when the Power Up is active. First, create an Empty gameobject and name it “Triple Laser”. Then, drag in a Laser prefab as a child of it, adjust the position, duplicate the Laser two times, and adjust the positions. With the Lasers in place, create a prefab of the Triple Laser.
In the Player script, add a private reference for the Triple Laser Prefab named “_tripleLaserPrefab” and a private bool named “_isTripleShotActive”. That will be what changes what the Player fires.
Next, in the FireLaser method, create an if statement to check if _isTripleShotActive is true and if it is Instantiate the _tripleLaserPrefab and if not, Instantiate the _laserPrefab.
Then, create a new public method named “TripleShotActivate” and inside of it, create an if statement that checks if _isTripleShotActive is false and, if so, set it to true. With that, the Player script is good to go.
In the PowerUp script, create a private float “_speed” with a value of 4. Then in Update, Translate the transform downward multiplied by _speed and Time.deltaTime. Next, create an if statement that checks the position on the y-axis to see if it is less than -5.5 and if it is, Destroy the gameobject.
Then create an OnTriggerEnter2D method and create an if statement to check if the other collider has the “Player” tag; if it does, get the Player script component and store it in a Player named “player”. Next null check player, and if it is not null, call the TripleShotActivate on player.
In the SpawnManager script, create a private variable for the Triple Shot PowerUp prefab with a null value.
Rename SpawnRoutine to “SpawnEnemyRoutine” and then create a new coroutine named “SpawnPowerUpRoutine” that will be almost the same as the SpawnEnemyRoutine but will Instantiate a PowerUp every 3–8 seconds instead of an Enemy every 5 seconds.
Now that the code is in place time to hook up the Prefabs. Triple Laser Prefab to the Player and Triple Shot PowerUp Prefab to the Spawn Manager.
The PowerUp is spawning, collectible by the Player, and activates the Triple Shot on the Player. The only problem is that the Triple Laser gameobject is not destroyed when the Lasers move off the screen.
In the Laser script, in the if statement inside of Update that checks the position on the y-axis, create an if statement that checks if the Laser has a parent gameobject and if it does destroy the parent gameobject, else destroy the Laser.
With that, the Triple Laser is cleaned up with the Laser.
The following article will look at having the Triple Shot effect turn off after an amount of time.