I feel like this is a real weak spot from Unity's tutorials. It requires a custom script which I will show you how to make, explain how it works, but I'm also simply giving it to you in case you find scripting daunting.
UPDATED Script Downloads:
https://drive.google.com/file/d/14DCB...
This covers and expands on the Unity Tutorials:
https://learn.unity.com/tutorial/intr...
&
https://learn.unity.com/tutorial/anim...
I'm using Kenney.nl's Toon Characters 1, specifically the female adventurer
I’ll take all the same steps from the previous tutorials:
Sprite Mode: Multiple
Pixels Per Unit: 16
Filter Mode: Point (no filter)
Format: RGBA 32bit
Apply
Use the Sprite Editor
Slice by Cell Count: 5 Rows 9 Columns
As an example, I drag all sprites into the scene and call this ‘AllSprites.animation’ to see what it looks like. We will NOT be using all the sprites to do this.
Rather than using them all at once, we can get specific about which sprites to use for specific actions.
Let's tackle Keyframes.
Drag a single sprite on screen.
Go to Window- Animation- Animation to open the Animation window.
Dock this window behind the Assets window so that it doesn’t clutter our workspace. This window might have already been docked behind the asset tab by another user.
As this is a single sprite we will not be prompted to create an animation.
Instead, with the sprite selected in the Hierarchy, we will click ‘Create’ in the Animation window
Add Property- Add Position
Press Record button
Move to another spot in time
Change a value (Change Y for jumping)
Stop recording
Play
Create another keyframe back at first position
Playtest (endless loop)
Creating an Idle Animation
Select Sprites #1, #13, #14, and #24 and drag them into the scene.
(To select more than one sprite hold down CTRL while selecting)
Name this animation ‘fa_idle.animation’
Playtest to show how quickly this animation plays
Show the different between the Controller and the Animation Clip
So far in all our tutorials, these automatically generated animations have looked good, but now they are becoming problematic.
Select the animated character in the scene.
Open up the arrow next to the layer in the animation
These sprites are animated as 1/12th of a second.
You can reposition these frames and you can even copy/paste them to make your idle animation
Creating Animation States
Go to Window- Animation- Animator to open the Animator window
The Entry is when the object enters the scene and it automatically plays the ‘Default State’
If you have multiple states you can transition between them.
Let’s create a second animation to show how this is done.
Select the first 7 of the last 9 sprites in the sprite sheet.
Drag these into the scene and create a new animation, call it fa_walk
Collapse the sprite sheet in the assets
Delete the character you just created to make this animation
Go back to the Animator
Select the character with the idle animation
Drag the fa_jump animation into the Animator to add the state
At this point we can transition from one state to another state, but this is all done sequentially. In order to have interactive, non-linear changes, we will need to use a script.
Here we can switch our default state
We can add transition from one clip to another clip
Right click a state and select ‘make a transition’ and drag this to the other state.
There is no transition back so this state will then continue forever
It is possible to transition back
It is possible to trigger these animations with scripts and to use parameters as conditions to allow these triggers.
Make sure Idle is our default animation.
Add a transition from Idle to Walk
We will create a new parameter called ‘walking’ which will be a Boolean set to false.
This transition from Idle to Walk will have the condition of walking True
This transition will then happen when this parameter is true.
Turn off Exit Time
Set Transition Duration to 0
Bad UI design if the game doesn’t respond quickly
This Script is preset with a series of parameters to allow you to switch animations when specific keys are pressed.
Download aniTriggerv2.zip, extract the .cs (C#) file from the zip and drag this script into my Unity Assets folder.
Drag this script onto the character’s inspector window, below all the other scripts/actions.
This script isn’t the most efficient, but it is the quickest/shortest script to make these transitions work.
This should now allow your character to transition from Idle to Walk when the D key is pressed.
However they will not transition back
We need to add a transition from Walk to Idle with the condition of walking false to allow this transition back.
You are now all set to make your animations and have them triggered through this script. Let me open this script to show you how it works, so you can make modifications if you want more functionality or in case you prefer to use different keys.