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.
Given below is a detailed guide on how to set up keyframe animation in Unity.
Create the GameObject of your choice.
Select the GameObject.
Go to Window > Animation > Animation.
It opens up an animation window where you can create and set up new animations.
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.
Select the properties of the GameObject that you are interested in modifying, such as position, scale, or color.
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.
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.
Click on the red circle again to disable the keyframe animation once you are done setting the keyframes.
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);}}}
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. animatorComponent
holds a reference to an Animator
component, which is used for controlling animations. WRAP_OUT
is a constant string with the value WrapOut
: the name of the animation. WRAP_IN
is another constant string with the value WrapIn
: the name of the animation.
Lines 11–14: The Start()
method is a Unity callback function that runs once when the GameObject is created. The animatorComponent
variable is assigned the value of the Animator
component 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 I
key is pressed (KeyCode.I
), the Animator
component's Play()
method is called with the animation state WrapIn
.
If the O
key is pressed (KeyCode.O
), the Animator component's Play()
method is called with the animation state named WrapOut
.
Drag and drop the C# from the project window onto the GameObject.
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.
Given below is a demonstration of an animated cube that is controlled by the WrapIn
and WrapOut
animations.
The WrapIn
animation is used to wrap in the GameObject.
The WrapOut
animation is used to wrap out the GameObject.
The sample project is based on the instructions given in the slides above.
Press I
to wrap in the animation.
Press O
to 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