What is keyframe animation in Unity?
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.
using System.Collections;using System.Collections.Generic;using UnityEngine;public class CubeController : MonoBehaviour{public Animator animatorComponent;public const string WRAP_OUT = "WrapOut";public const string WRAP_IN = "WrapIn";// Start is called before the first frame updatevoid Start(){animatorComponent = GetComponent<Animator>();}// Update is called once per framevoid Update(){if (Input.GetKey(KeyCode.I)){animatorComponent.Play(WRAP_IN);}if (Input.GetKey(KeyCode.O)){animatorComponent.Play(WRAP_OUT);}}}
Explanation
Lines 1–3: These lines make all the necessary imports.
Line 5: A class is declared namedÂ
CubeController. It inherits methods from the classÂMonoBehaviour, a base class in Unity for scripts attached to the specific GameObjects.Lines 7–9: These public variables are declared here.
animatorComponentholds a reference to anAnimatorcomponent, which is used for controlling animations.WRAP_OUTis a constant string with the valueWrapOut: the name of the animation.WRAP_INis another constant string with the valueWrapIn: the name of the animation.Lines 11–14: The
Start()method is a Unity callback function that runs once when the GameObject is created. TheanimatorComponentvariable is assigned the value of theAnimatorcomponent attached to the same GameObject as the script.Lines 17–27: The
Update()method is another Unity callback function that runs once per frame. Here, the script checks for input from the user.If the
Ikey is pressed (KeyCode.I), theAnimatorcomponent'sPlay()method is called with the animation stateWrapIn.If the
Okey is pressed (KeyCode.O), the Animator component'sPlay()method is called with the animation state namedWrapOut.
Assign C# script to the GameObject
Drag and drop the C# from the project window onto the GameObject.
Play and test the animation
Save all the changes.
Click on Play to play the animation.
You would be able to see the animated GameObject based on how you have set the keyfrmes.
Demonstration
Given below is a demonstration of an animated cube that is controlled by the WrapIn and WrapOut animations.
The
WrapInanimation is used to wrap in the GameObject.The
WrapOutanimation is used to wrap out the GameObject.
Sample project
The sample project is based on the instructions given in the slides above.
Press
Ito wrap in the animation.Press
Oto wrap out the animation.
import React from 'react';
require('./style.css');
import ReactDOM from 'react-dom';
import App from './app.js';
ReactDOM.render(
<App />,
document.getElementById('root')
);
Free Resources