Doers of Stuff.org

A place to Do Stuff and see Stuff Done…

Animating Sprites in Unity

One of the reasons I am so excited about learning game development is because it is forcing me into entirely new realms. This is more than changing languages, paradigms, frameworks or any of the usual shifts a “normal” programmer will experience. As such, this is an article I will likely bookmark myself and keep coming back to, because now I am starting to mess with the Animation system.

Okay, this first bit is not really going to be that earth shattering, but even in its simplest form I am finding myself on totally new ground. Even the vocabulary is new. So, let’s jump in!

First thing to know, is you cannot add an Animation to a prefab. At this point, I’m not even going to pretend to know why, nor will I go into my usual, dig-into-something-I-don’t-need-to-know-just-because-I-can mode. I’m just going to roll with it. What this means, is my first powerup prefab needs to be dragged back into the Hierarchy window so I can attach an animation to it. We then select the game object in the Hierarchy and from our main menu select, Window -> Animation -> Animation. Yes, I put “Animation” twice there. There is a difference between “Animation” and “Animator” so be sure to select the right one.

From Unity’s manuals:

Unity’s animation system is recommended in most situations and provides better performance for complex animations with many animation curves and blending. The Legacy Animation system can provide better performance for simpler animations with few animation curves, such as simple UI animations. You can use both systems in your project.

https://docs.unity3d.com/Manual/AnimationOverview.html

So, for this exercise, we will actually be using the legacy Animation system.

Anyway, selecting the Animation menu item will open what is called a Dope Sheet (see what I mean about the vocabulary?)

You can dock this window somewhere for convenience if you want. If and where are a matter of your screen real estate and how much you want to see at the same time. Regardless, there is quite a bit you can do in here that we are going to gleefully skip over for right now, because our first powerup asset came prepackaged with what we need.

What we need is each frame of our animation saved as a separate image file.

I believe the numbering system is important because when we drag these all into the Animation window, it will use that to keep the images in order. You will notice, when we first select the object in the Hierarchy we will be animating, Unity shows a blank pane with the following:

Clicking on the “Create” button will allow us to get started. The animation will be stored in the Assets folder and like other types of assets, it makes sense to create a folder just for animations and save this empty animation in the new folder.

This next part might seem a bit odd at first, but once you drag all the individual files into the Animation window, you then hit the record button. If it looks good, stop the recording. You even hit the play button if you need to see it again.

The control buttons should be familiar. They work as a toggle. Press the button once to start the action and again to stop it. So, the red record button is both start and stop. You might also notice you have other controls. The box with the number is the current frame. You can change that to hop to the frame you want. The slim, white, vertical bar also shows the current frame. You can grab this with the mouse and move it as well. Regardless of how you do it, the effects will be visible in your scene and game view.

There is actually a bit more going on, but we can actually ignore it for now and come back to the other parts when we plan to actually do something interesting with them.

Oh, and don’t forget to apply the Overrides in your game object to save it to the prefab and then of course, remove the game object itself from the Hierarchy.

Leave a Reply

Animating Sprites in Unity

by Robert time to read: 3 min
0