There are three main parts to using animations in Unity:
- Animation - Essentially the video clip you will play
- Animator - This is a component you put on the character. It is a flowchart that handles which animation clip to play when.
- A Script - This is the main way to control the Animator, though there are ways to animate your character without any code.
Open the Animator and Animation Windows
From the top menu, make sure that both your "Animation" and "Animator" windows are open.
- Top Menu --> Window --> Animation --> Animation
- Top Menu --> Window --> Animation --> Animator
Giving the Player an Animator Controller
- In the "Project" window, right click and select
- Right Click --> Create --> Animator Controller
- Give it a clear name, such as "PlayerAnimator"
- In the "Hierarchy" window, select the GameObject you wish to animate. Add the component "Animator".
- With the character selected, you will notice that the field "Controller" currently says none. Drag your "PlayerAnimator" into this field.
- All you need now are some Animation Clips to play
Note: Unity will automatically do this for you when you create your first Animation Clip (next step) while selecting a GameObject in the Hierarchy. I just feel like it is helpful to understand how to do it yourself.
Creating an Animation Clip
- While selecting the player GameObject in the Hierarchy, look at the "Animation" window
- In the middle of the Animation window is a "Create" button. Click this button and give your new animation clip (like a video clip) a clear name such as "Player_Idle"
- Organize your new Animation Clip and Animator Controller in your Project folder, so it isn't cluttered. Such as creating an "Animation Clips" folder, with subfolders for each different animated character or GameObject.
- Notice two things:
- Your "PlayerAnimator" now has a new "State", or animation clip, to play in its flowchart
- Your Animation window now has a timeline and options for this specific animation clip
Recording an Animation Clip
You can directly record changes to most any attribute of any GameObject component. For example, you can have the x- and y-scale of your character change in an animation to make it wobble a little bit. You can change the z-rotation in an animation to make a wheel spin. You can change the color of the sprite renderer over time to show sunrise to sunset. Below is a brief explanation of how.
- Select the appropriate GameObject in the Hierarchy, and look at the "Animation" window. Make sure the correct animation is selected (in the top left of the Animation window, under "Preview").
- Click "Add Property" and select the property you wish to change over time in your animation. For example, you want a tree to gently sway from side to side, so you select:
- Add Property --> Transform --> Rotation (click plus sign)
- Click the red circular "Record" button
- Click your mouse on the timeline where you want to set a value. For example, I want to set the rotation at the starting time (0:00), so I click on 0:00 and the tall white slider bar goes to that point in the animation clips timeline. Then go up to the Transform and set the rotation values.
- Note: The timeline is in 60ths of a second. So 0:30 is half of a second. 1:00 is one second.
- Click on an ending (furthest rotation) value in time. For me, I will click on 1:00 (one second into the animation timeline). Go up to the Transform component and change the rotation to the end extreme value of your desired rotation. For me, i will tilt the tree 10 degrees.
- Repeat for all the key endpoints. Mine are below:
- 0:00 - rotation (0,0,0)
- 0:30 - rotation (0,0,10)
- 1:00 - rotation (0,0,0)
- 1:30 - rotation (0,0,-10)
- 2:00 - rotation (0,0,0)
- Click the red circular "Record" button to stop recording when you have finished.
Hit play to see it in action. Since this is the only animation you have (you don't have a static unmoving animation clip), your player will start the game doing this animation on loop.
Editing the Tweening Curve
Unity will automatically smooth out the "tweening" between the key values that you set when recording your animation. In other words, the animation will automatically rotate the tree smoothly from (0,0,0) to (0,0,10) in a half of a second)
If you don't want Unity to automatically choose "tween" method to use to smoothly go between your key moments in the animation clip, you can edit the curve yourself.
I find this useful to smooth out the start and end of my animation clips, in case the loop doesn't look smooth going from run to jump, or rotating one way to resting.
Some Great Animation Resources:
Brackey's Videos on Animation
BlackThornProd Videos on Animation
Google Slides I made Last Year
These walk you through how to navigate between animations using scripting. One thing I never got to adding to the slides are "Trigger" parameters. Triggers are essentially just boolean values that automatically switch back when the animation is done. This can be really useful for things like "attack". Trigger the onetime animation clip and let it automatically untrigger. It is a parameter datatype just like boolean or float, and you code it the same way except for you don't need to pass in a value. Instead of "anim.SetBool("walking", true);" you simply write "anim.SetTrigger("Attack");". You still need to create transitions in your Animator that use this as the condition.
Skeletal Animation is Here!
Published Nov, 2018. You need 2018.3 or better to use these tools.
Some Great Animation Resources:
Miscellaneous Other Videos
- "Animation 101 - Intro to Animation in Unity" by Makin' Stuff Look Good - A 10min video from March 2016 that walks you through much of what is in the tutorial above.'
- "Playing Animation on Trigger in Unity" ( 9min, Jan 2019) - A YouTube tutorial by SpeedTutor that goes through how to plan an animation when you land on a trigger (OnTriggerEnter) using a Boolean variable. Walks you through much of what is in the tutorial above in a quick smooth fashion.
- "Rig and Animate 2D Characters in Unity 2018" (23min, Oct 2018) by Sykoo. A video on how to rig up a skeleton 2D character made of many sprites, then animate how they move together.
- "How to Create Animations from Models and Sprites within Unity" - A webpage tutorial on GameDevAcademy.com by Jesse Glover. It is a very thorough walkthrough using a few 3D humanoid models.
- "Simple 2D Sprite-sheet animations in Unity" - A webpage tutorial on gamecodeschool.com. It walks through turning a 2D sprite sheet into an animation, the coding, the sprite editor and the animator flowchart.
BlackThornProd Videos on Animation
- How to Make 2D Game Animations in Unity - Beginner Tutorial (15min, Feb 2019)
- Making Run, Idle & Jump 2D Game Animations (15min, Feb 2019)
- How to Make Great Game Environments (10min, Sep 2018)
- How to Make Animation Transitions (9min, May 2018)
- Playlist of at least 12 videos on how to create Game Art and Animations
- 6 Details to Make a Great Player Character (9min, Jul 2018)
- How to Animate Using Sprite Sheets (8min, Jun 2018)