Unity has a function called Animatorcontroller, which is used to implement animation effects for characters and 3D models.
AnimatorController controls multiple animation clips, and by using this function, you can switch animation clips, and combine different animations.
In this article, I am going to introduce how to use AnimatorController through a tutorial to create a rotating cube animation.
Be aware that you need basic knowledge about the animation function beforehand, so if you haven’t read this ‘[Introduction to Unity] Learn Animation system’ article yet, please read it first.
Open the AnimatorController
As preparation, please create a Cube.
You can keep the settings as default.
First, go to the Project view and select「Create」→「Animator Controller」.
A new file is created in the Assets folder. This is the AnimatorController file.
For this tutorial, I want to create an animation where the object simply rotates, so please change the AnimatorController’s name to ‘Rotate’.
Double click the AnimatorController file, then a Animator settings window will popup in the Scene.
Creating an Animation
Let’s create a Rotate animation then.
In the Project view, right click and select「Create」→「Animation」.
An animation clip will be created in the Assets folder, so change the file name to ‘Cube Rotate’, and drag and drop the file into the Cube’s Inspector window.
This procedure is needed because the file does not work on it’s own, it needs to be attached to the object it affects (The Cube).
An animation has been added to the Cube’s Inspector window.
But although the animation is set to the Cube object, the animation is still empty now.
So we need to add a rotation movement to the animation.
Click the Window menu, select Animation, and open the Animation view.
In the Animation view, select「Add Property」→「Transform」→「Rotation」, and click the + button.
As in the image below, a timeline will be displayed, so change the keyframe value of Rotation.y 1:00 to 360.
This settings makes the Cube rotate once (for 360 degrees) around the Y-axis every second.
Run the Scene.
As planned, the Cube rotated once around the Y-axis. But the animation stops after one rotation.(The above is a GIF image, so it will rotate continuously)
Create a State
Next, I will explain how to use the AnimatorController.
Right click in the Animator window, and select「Create State」→「Empty」.
A rectangle called ‘New State’ has been created.
The rectangles in the Animator window are ‘States’, including ‘New State’.
A State is a function that can store and set settings to animation clips. By adjusting the settings of a State, you can edit the animation settings.
Currently, a arrow is emerging from the ‘Entry’ State and heading towards the ‘New State’ State. This arrow is called a Transition.
A transition is used to set the execute order of animations, so in the above image, the ‘Entry’ animation runs first, then the ‘New State’ animation.
Role of each State
Now, I will explain the basic roles of each default State.
The Entry State is the first State that is called, and the transition happens from here.
You need to set a transition from Entry to the animation you want to run first.
The Any State is a State that can transition from any State to any State.
So, if you set a transition as in the image below, you can set any transitions you want to.
If you transition to the Exit State, the transition returns to the Entry State.
So, if you set the last transition to the Exit State, you can loop your animations.
How to use AnimatorController
Once we have learned about each State, let’s select the ‘New State’ we created in the previous step and change the name to ‘Cube Rotate’.
Looping the Animation
Next, let’s set a loop to the animation by editing the State settings.
Right click the ‘Cube Rotate’ State, and then click ‘Make Transition’.
A white arrow will emerge, so attach it to the ‘Exit’ State.
The transition returns to the Entry State after the Exit State, so the animation will loop with this setting.
Run the Scene and check the animation.
You can see the animation is looping now.
In order to explain how to set an animation in Unity, I made a very simple tutorial for this article.
Even if an animation that looks complicated, it is just that there are many objects included, or movements are set in detail.
I want to introduce more advanced uses of the AnimatorController, but there isn’t any more space, so that is it for this article. I hope you look forward to my next article!