This article is part of tutorial about making games, read it here.
In previous lesson I explained how to create animations for our humanoid person model.
We now have not only our model, but also animations for all the needed states.
To make it all work, we first need to import animations to Unity, then set proper transitions in State Machine, and finally write the code and test the changes.
So lets start.
First Unity Run
Open Unity Hub, make sure you’re logged in, and have installed the Editor.
Creating New Project
Click New, select 3D, and give our game a meaningful name, such as My First Good Game.
This template means exactly empty project, which is what we want. Other templates contain extra features, that we don’t need.
You can read more about templates here.
Click Create. Unity may take few minutes or more to generate the project.
Recreating Our Scene
This step is optional, but I like to always delete the scene that comes with fresh project, and recreate it.
Not only you get to choose its name, but any eventual settings other than default are restarted, which gives us a fresh start.
Right-click SampleScene on the left in Hierarchy window:
And click Add New Scene.
Now we can delete our old scene, so right-click it SampleScene and press RemoveScene.
Now right-click our new scene called Untitled, and click Save Scene, then again give it a meaningful name. There’s a chance our old SampleScene is still in the folder if Unity is lazy, so remove it to keep things clean.
I will call our new scene Rookgaard, as it will be our starting island inside the game. Save the file.
Each scene can mean as much as one island, as one level in dungeon, as one map, or as one area, or one city. You can categorize them however you want.
I don’t recommend creating whole map of huge open world game in one scene. That may lag not only the editor, but also the game.
Transitions between scenes can be either normal (showing loading screen or something) and seamless (literally not showing anything, and player feels like hes still in the same world, but in the game he entered second scene).
You could later create the transition (of any kind) for example when player took a flight / entered a dungeon / died / moved far away from starting point, and so on.
For now lets not worry about the transitions, as we will create huge part of our game in Rookgaard.
Creating Temporary Floor
We will be creating highly detailed landscape later, full of mountains, caves, trees and buildings, however we first need a character, and to test if it can walk properly, we will add temporary floor.
In the top bar, click GameObject > 3D Object > Plane to add a plane object, which will work as our floor.
Click on the Plane in Scene view, which is on the center and displays our game in 3D perspective.
We need to change its scale, because its too small and character may fall into the abyss.
Change the Scale from 1, 1, 1 to 5, 1, 5.
Importing & Adding Animations
We are more than ready to import our animations.
We will be only importing the animations without the model we made in Blender, because Mixamo’s automatic rigger included the model in them.
So drag them from desktop to Animations folder.
Creating Measurement Cube
Default cube size is one meter. We need it because models, when imported to Unity, may have stupid big size, that is equal to 100+ meters in Unity, and that may lag especially when we add physics.
Our moving 2m tall character would be as CPU-intensive as 100m skyscraper falling.
Add Cube the same way we added Plane. And change its Position to 1.5, 1.5, 1.5. Leave default Scale.
You can later remove the box if you want.
Creating Stickman Character
Drag our rigged model (that we downloaded as first) to Hierarchy. And rename it to Character.
That should spawn our stickman on the map.
Select Character and change Scale to 0.1, 0.1, 0.1.
Change his Position to 0, 1, 0.
Depending on the model you used, the numbers you need may be little different.
Play with them to get this effect where character is not taller than the height of two boxes, and he stands directly on the ground instead of being in the air.
Now we need to add the animations to our character. Expand Idle and select animation file.
Now press Ctrl + D to duplicate it.
We are doing it because of two reasons:
- To extract animation from model.
- To avoid read-only setting, which Mixamo created.
After you extracted it, rename it to Idle.
Do the same for other two animations.
Now select all the renamed animations, and drag them to Character in Hierarchy.
- Create Character Controller file and set it.
- Add Animator component.
- Assign animations to our character.
Now we can set the transitions so Unity knows when it can play particular animation.
Setting Animator Transitions
Enter Animator window that by default is at the bottom.
You can see for each animation, Unity created a node in Animator.
You can drag an arrow line from one node to another.
Rearranging Animator Nodes
This is what you want:
So rearrange your nodes and make them like this.
Creating Boolean Parameters
Now go to Parameters and create two boolean parameters for Running and Jumping that are false by default.
These parameters work as conditions that decide whether animation can play or not. They also will be referenced from our code so they work like a middleman between our C# code and Unity’s Animator nodes.
Adjusting Transitions’s Settings
Now its time to adjust Animator transition settings.
Set options Has Exit Time, Exit Time, and Transition Duration to be like on screens. Also set Conditions below. You can then find explanations of these options below screens.
Idle to Running:
Running to Idle:
Idle to Jumping:
Jumping to Idle:
Running to Jumping:
Jumping to Running:
Idle to Idle:
Running to Running:
Jumping to Jumping:
So let me explain these settings a bit.
Has Exit Time determines whether transition from one node (so animation) to another can happen during the animation or after it finished playing. If set to true, it means it will wait for the animation clip to finish.
This is useful for example when we are in the air after a jump – we don’t want running animation to play even if we press the arrow keys on keyboard.
Transition Duration determines how smooth is the transition. If set to 0, then transition from idle to running will be instant and it will look very sharp, it won’t blend in. And that’s not what we want.
We want a clean animation that switches from one clip to another without showing any irregularities.
And this is what Transition Duration is for. And same applies to the Exit Time setting.
The only difference is that Exit Time changes how smooth is exit from first animation, while Transition Duration determines how smooth is entering second animation.
Same thing, different animation.
In next lesson we will be coding movement, rotation and animation using Character Controller API and Animator’s parameters.