Keyframe animation refers to a method of animation where specific frames, known as keyframes, are defined to represent different stages of an animation. Unity smoothly interpolates between these frames, creating the illusion of a change or movement.
Steps to create keyframe animation
Given below is a detailed guide on how to set up keyframe animation in Unity.
Select the GameObject
Create the GameObject of your choice.
Open animation window
Select the GameObject.
Go to Window > Animation > Animation.
It opens up an animation window where you can create and set up new animations.
Create new animation
Click on Create button to create a new animation.
It prompts you to choose the name and location of the file.
Note: In the latest versions of Unity, creating a new animation creates an animator controller as well so, there is no need to set up animator controller separately.
Add properties
Select the properties of the GameObject that you are interested in modifying, such as position, scale, or color.
Enable animation recording
Click on the red circle named Enable/Disable Animation Recording button.
It is used to define the keyframes for your animation.
It allows you to record any changes you make to your GameObject.
Set keyframes
Drag the timeline cursor to the time where you want to change the properties of the GameObject.
You can directly modify the properties of the GameObject from the scene view.
Unity will automatically record this as a keyframe.
Disable animation recording
Click on the red circle again to disable the keyframe animation once you are done setting the keyframes.
Create a C# script
Create a new C# script to trigger the animations.
A sample C# script is given below.