Section: Glitch Garden
A while ago I wrote up a Udemy discussion that dealt with using Unity's animation tools. This was meant to alleviate a little learning road bump in the course and several students can personally vouch for its 100% efficacy.
Nikola Tesla was an inventor, a man ahead of his time and a veritable fallen angel; a true light-bringer. Also dead.
And me? I wrote 10 simple steps that help you move a fox across a screen.
That post has 10 likes.
But like all great things, its glory must come to a close. In the wake of recent re-records, I hope this blog post can stand as a more permanent and robust explanation of Sprite-based animation in Unity.
The Animator Component, SpriteRenderer Component and Animation Basics
We've seen both the Animator and SpriteRenderer Components before.
How does animation work? A movie is an illusion. It's simply a series of still photographs. Each consecutive photograph contains an incrementally small change in the position of objects. When you project these photographs one after the other quickly enough, you get the illusion of motion; a moving picture or movie. In an animated film you replace 'still photographs' with 'hand-produced pictures' and achieve the same effect.
We can accomplish the same thing in Unity.
- Instead of photographs, we use sprites (image files).
- Instead of projecting to a movie screen we project to the SpriteRenderer.
- And instead of a projector we have the Animator that does all the heavy lifting of switching between sprites (among other things).
The Animator works with the SpriteRenderer to play an animation clip.
This explains a problem you might have encountered: When you set up your GameObject with an Animator but no SpriteRenderer, a SpriteRenderer may be automatically added when you try to play an animation clip. This is because you cannot just project a sprite on a GameObject! You also need the movie screen.
The Parent-Child Model and Capabilities of the Animator Component
If you place an Animator on a parent object, then it has access to components on the parent object AND components on any children.
An Animator on the trunk can reach into any branch and affect certain components including the SpriteRenderer!
What I mean is that the ratio of Animator to SpriteRenderer is not necessarily one-to-one. It can be one-to-many. Unlike a real projector, a single Animator can play clips on several different SpriteRenderers at the same time.
Now that we understand the capabilities of the Animator, we can set up a GameObject accordingly.
- The parent object gets an Animator component.
- Any child objects receive a SpriteRenderer.
Let's try this now with a Fox. Create 2 Empty Game Objects. Name one 'Fox' and give it an Animator. Name the other 'Body' and give it a SpriteRenderer. Child Body to the Fox.
The rest is simply a matter of adding Animation Clips to that curve.
A More Complex Example
So for the most part, any animated GameObject has a single Animator Component that controls all the animation states.
The Fox & Body example is simple but it's not hard to imagine a more complicated GameObject requiring multiple children. The AIThirdPersonController (provided in the Unity Standard Assets) has dozens of animated children but only one Animator component at the top level.
I'm Elijah Lape, and I'm taking the Complete Unity Developer course, Complete Blender Developer course, and Game Physics course. I enjoy making things.
You can read my other posts on the blog