Importing animations to game engines can be tricky if you are doing it for the first time or didn’t do it in a while. Especially setting them – you have to create Animator Controller, set transition nodes, connect them with each other, and finally write code that will call them. It may sound easy but Unity’s State Machine can’t be taken lightly, you don’t want to see your character running when he’s in the air after all.
This article is a part of bigger guide to making your first video game. If you want to learn how to make models in Blender, code scripts in C# and to learn Unity to the point where you can produce 3D games in it, then go give a read to my RPG game design tutorial – it focuses on creating role playing game in medieval style, but following this tutorial will make you learn Unity and Blender that you can later use to make any kind of game!
This article is about setting Animator and State Machine, in our previous chapter we went through creating idle, jumping & running animations for our player. If you didn’t read it yet, then go give my 3D character model movement animation tutorial a try. This way you can get smooth movement animations that will work with the humanoid character model we made before.
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 let’s start.
Table Of Contents
Running Unity Engine For First Time
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 will lag not only the editor, but also the game.
Transitions between scenes can be either normal (showing loading screen) and seamless (literally not showing anything, and player feels like hes still in the same world, but in reality he has entered second scene).
You could later create the transition (of any kind) for example when player took a flight, entered a dungeon, died, or moved far away from starting point. For now let’s not worry about 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 Character 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, and that may lag especially when we add physics.
In such case, our 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 has created.
After you’ve 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. That will:
- 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 Between Animations
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 C# code and Animator nodes.
Adjusting Transitions’s Settings
Now it’s 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.
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.
We’ve just set our State Machine transitions between animation nodes correctly. However our character still can’t walk, run or jump. Now it’s time to write a code that will interact with player model and animations (or rather with State Machine nodes, transitions and parameters) so that when we press one of W, A, S, D buttons our character will actually start running. Head to next chapter about programming character movement, rotation & animation scripts using Character Controller’s API and Animator’s parameters. In this chapter we will also learn the basics of C# and how to code our first Unity scripts.