Animation clips let you create and save movements or changes for objects in your game. They help bring characters and scenes to life by showing actions over time.
0
0
Animation clips in Unity
Introduction
When you want a character to walk, jump, or wave their hand.
When you want a door to open smoothly instead of instantly.
When you want to animate UI elements like buttons or menus.
When you want to create repeating background animations like waving flags.
When you want to combine different movements into one sequence.
Syntax
Unity
AnimationClip clip = new AnimationClip(); clip.legacy = true; // Add keyframes and curves to clip Animation animation = gameObject.AddComponent<Animation>(); animation.AddClip(clip, "clipName"); animation.Play("clipName");
AnimationClip stores the animation data like position or rotation changes over time.
You can create clips in code or import them from external tools like Blender.
Examples
This example creates an animation clip that moves an object along the x-axis from 0 to 10 units in 1 second.
Unity
AnimationClip clip = new AnimationClip(); clip.legacy = true; // Create a simple position animation AnimationCurve curve = AnimationCurve.Linear(0, 0, 1, 10); clip.SetCurve("", typeof(Transform), "localPosition.x", curve);
This code adds the animation clip to a game object and plays it immediately.
Unity
Animation animation = gameObject.AddComponent<Animation>(); animation.AddClip(clip, "moveRight"); animation.Play("moveRight");
Sample Program
This script creates an animation clip that moves the object 5 units to the right over 2 seconds. It adds the clip to the object and plays it when the game starts.
Unity
using UnityEngine; public class SimpleAnimation : MonoBehaviour { void Start() { AnimationClip clip = new AnimationClip(); clip.legacy = true; // Animate localPosition.x from 0 to 5 over 2 seconds AnimationCurve curve = AnimationCurve.Linear(0f, 0f, 2f, 5f); clip.SetCurve("", typeof(Transform), "localPosition.x", curve); Animation animation = gameObject.AddComponent<Animation>(); animation.AddClip(clip, "moveRight"); animation.Play("moveRight"); } }
OutputSuccess
Important Notes
Remember to set clip.legacy = true when using the Animation component for clips created in code.
Animation clips can animate many properties like position, rotation, scale, colors, and more.
You can preview animation clips in Unity's Animation window before running the game.
Summary
Animation clips store movements or changes over time for game objects.
You create clips by defining curves for properties like position or rotation.
Use the Animation component to play these clips on your objects.