Animation Clips are one of the core elements of Unity’s animation system. Gameobjects in Unity can be animated using animation Clip. Using Animation Clips, we can change position, rotation and scale of a gameobject as well as other properties like float or integer value of material from the script.
In Unity, Animation clips are created using “Animation“ window, under Windows from the main menu -> Animation. You can see an animation window like the game view and scene view in unity editor as shown below :
All animations in Unity are handled by the Animation Controller Component. This is the component that references to 1 or more animation clips to animate a gameobject.
To create an animation clip to scale a gameobject we need to modify the scale property of that gameobject. Create a Cube. Select that Cube in the hierarchy, and you can see there is a button in Animation window “Create” to create a new animation clip as shown in the picture :
By pressing the create button in the Animation view, a new animation clip is created. Unity will ask to pick the name & location to save this new Animation Clip. Unity also creates an Animator Controller asset with the same name as the selected GameObject, adds an Animator component to the GameObject, and connects the assets up appropriately. Give the name Scale and save the clip. Unity will automatically create an animation clip named “Scale_Anim“ and an animator controller named Cube. It is the name of the currently selected gameobject. The animation clip contains the .anim extension. Now can see animation window with frames.
Now select the gameobject Cube and click on “ Add Property ” button.Select scale form Transform and press “ + ” button to create scale a property. The upper part number defines the playback duration and time line of the animation clip.You can click anywhere on the Animation window time line to move the playback head to that frame, and preview or modify that frame in the Animation Clip. The numbers in the time line are shown as seconds and frames, so 1:30 means 1 second and 30 frames.Default keyframes are created from 0:00 to 1:00. We can add new keyframes by right clicking in the animation window, you will see a menu and then select Add key. You can drag this key anywhere in the animation window to create an animation clip.
There are 2 modes of animation window to create an animation clip :
Dopesheet allows us to create animation clips with some exact specific values for the gameobject.
Curves allows us to create animation clips with curves value.
When you are creating an animation clip, the animation window will record the animation. The red button at the top left corner is highlighted. In this tutorial, we will use Dopesheet to create an animation clip.
We will scale cube from (1,1,1) to (2,2,2) using animation clip. Select the first keyframe at 0:00 and then select cube from hierarchy and set scale value from Transform component to (1,1,1). This will set the value of cube scale to (1,1,1) at the beginning of the clip. Now add a new keyframe at the time 0:50 and set the scale value of cube to (2,2,2). This will set the cube scale to (2,2,2) in the middle of the animation. Now select the last keyframe and set the scale to again (1,1,1). This will set the value at last of animation.
Now hit the play in the animation window to preview the animation. You can see scale animation of the cube. This animation clip is assigned as a default state in the animation controller.
At last, the keyframes value will look as shown in the picture :
Now hit the play button in Unity editor. You will see a Zoom-in Zoom-out animation for the cube.
Now create another clip for rotation.Click in the dropdown of animation list below the record button and then select Create New Clip. Save clip as Rotate_Anim.
We will rotate Cube from 0 to 360 degrees. Select the first keyframe at 0:00 and then select cube from hierarchy and set rotation value from Transform component to (0,0,0). This will set the value of cube rotation to (0,0,0) at the beginning of the clip. Now add a new keyframe at the time 0:50 and set the rotation value of cube to (0,0,180). This will set the cube rotation to (0,0,180) at the middle of the animation. Now select the last keyframe and set the rotation to (0,0,360). This will set the value at last of animation.
Now hit the play in the animation window to preview the animation. You can see rotation animation of cube. This animation clip is assigned as a new animation state in the animation controller.
To play both animations for the cube, we need to make a transition between these 2 clips. Add a transition from Scale_anim to Rotate_Anim in animator controller. To make a transition right click on the current state and select Make Transition and then drag towards the destination state and then release. You will see a transition with arrow line. Now add a transition from Rotate_Clip to Scale_Clip. It will look like as shown below :
Now hit the play button in the Unity Editor. You will see the scale animation and rotation animation of the cube.