Hey and welcome.
Now animation in Unity is a pretty big subject, in this article we’ll just be having a quick look into how we can use the built in animation feature in Unity to cycle through a sprite sheet.
First though let’s go ahead and put in some spawning functionality for our TripleShot powerup. As luck would have it we already have a script that handles our spawning for our enemies so it’s just a case of adding a little bit extra to our Spawner script:
private GameObject _tripleShot;IEnumerator SpawnPowerup()
while (_playerAlive == true)
Instantiate(_tripleShot, new Vector3(Random.Range(-10.7f, 10.7f), 6.2f, 0), Quaternion.identity);
yield return new WaitForSeconds(Random.Range(2.0f, 8.0f));
That’s all it takes, you’ll notice that our Instantiate part is pretty much copied straight from our SpawnEnemy() Coroutine since we’ll want the position of our powerup to act in the same way.
A cool thing we’ve got going here though is that we’ve put a Random.Range() in our WaitForSeconds(), that means that our yield isn’t going to be a fixed time and will instead pause things for a random amount of time between 2 and 8 seconds before it gets back to the while loop and instantiates a new powerup. The numbers of course can be adjusted to your liking and you can also apply the Random.Range() to our enemy as well if you wished!
We’ve got some solid scripting here now for our powerup. You’ll notice that my TripleShot powerup is hiding behind the galaxies, that’s because I forgot to set its sorting layer to the foreground.
Now let’s get some animation in our game, to do that we’ll need to make use of something called a sprite sheet.
A Sprite sheet is very common for older pixely games that relied on sprites and the idea is that when a spreadsheet is played in order to give off the impression that the sprite is moving. So usually it’s just the same sprite with slight adjustments here and there, a good example is the blue wonder here:
Anyone a fan of the old Sega Megadrive/Genesis games will recognise these animations. I bet there’s less to it than you initially thought right? It’s amazing what developers were capable of with just a few MegaBytes to work with.
Anyway I’m getting sidetracked, the point here is that if you look at the running sprites in order for example you’ll see that if they were in a flipbook it would give off the impression of movement.
Well that’s exactly what we’re going to be doing here for our TripleShot powerup since I have a number of sprites ready to make use of to give the illusion of a glowing effect.
First things first though we need ourselves an animation window by clicking the “Window” at the top and selecting the Animation > Animation option. I like to put the window down next to the Console and Game window so that I have easy access to the sprites in the Project window.
Right with the Animation window in place we can click on our TripleShot powerup and then the create option in our Animation window. Save the animation with an appropriate name in a relevant folder and then hit the record button.
If you’re dealing with a large project it’s handy to append a _anim to your animation name or anything similar so that you have an easy way to find your animations if you’re searching in the project window.
Now we simply grab all of our sprites making sure that we’ve numbered them in the order we want them to play and then drag them into our dope sheet and stop the recording:
With that we’ll have ourselves some fully functioning animation:
Now this is barely scratching the surface on what animations are capable of so once I start covering 3D game development I’ll be putting out an article I’ve been working on about getting animations from Adobe Mixamo working in Unity as well as looking into Unity’s built in animator feature to chain together the animations under certain conditions.