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.
Animation clips in Unity
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
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
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);
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"); } }
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.
Practice
1. What is the main purpose of an
Animation Clip in Unity?easy
Solution
Step 1: Understand what animation clips store
Animation clips hold data about how objects or characters move or change over time.Step 2: Compare with other options
3D models, scripts, and audio are handled separately, not by animation clips.Final Answer:
To store movement and transformation data for objects or characters -> Option BQuick Check:
Animation clips = movement data [OK]
Hint: Animation clips = movement data, not models or scripts [OK]
Common Mistakes:
- Confusing animation clips with 3D models
- Thinking animation clips handle scripts
- Mixing animation clips with audio management
2. Which of the following is the correct way to play an animation clip using the
Animation component in Unity C#?easy
Solution
Step 1: Recall the Animation component method
The correct method to play an animation clip by name isPlaywith the clip name as a string.Step 2: Check method names in options
Onlyanimation.Play("Run")matches Unity's API exactly.Final Answer:
animation.Play("Run"); -> Option CQuick Check:
Play method plays clips by name [OK]
Hint: Use animation.Play("clipName") to play clips [OK]
Common Mistakes:
- Using incorrect method names like run() or start()
- Using wrong capitalization like playClip()
- Confusing method names with other components
3. Given this code snippet, what will be the output in the Unity console?
Animator animator = GetComponent<Animator>();
animator.Play("Jump");
Debug.Log(animator.GetCurrentAnimatorStateInfo(0).IsName("Jump"));medium
Solution
Step 1: Understand what animator.Play("Jump") does
This command starts playing the animation state named "Jump" on layer 0.Step 2: Check if current state is "Jump"
The methodGetCurrentAnimatorStateInfo(0).IsName("Jump")returns true if the current animation state is "Jump".Final Answer:
True -> Option AQuick Check:
Play("Jump") sets state to Jump, IsName("Jump") = true [OK]
Hint: Play sets state immediately; IsName confirms current state [OK]
Common Mistakes:
- Assuming state changes after delay
- Confusing layer index in GetCurrentAnimatorStateInfo
- Expecting exceptions without null checks
4. What is wrong with this code snippet that tries to play an animation clip?
Animation anim = GetComponent<Animation>(); anim.Play();
medium
Solution
Step 1: Check Animation.Play() method signature
The Play method requires a string parameter specifying which clip to play.Step 2: Identify missing parameter
Calling Play() without arguments causes a compile or runtime error.Final Answer:
Play() requires the clip name as a parameter -> Option DQuick Check:
Play needs clip name string [OK]
Hint: Always pass clip name to Play() method [OK]
Common Mistakes:
- Calling Play() without parameters
- Confusing Animation with Animator component
- Assuming Play() plays default clip automatically
5. You want to blend two animation clips smoothly using the Animator component. Which approach is best to achieve this in Unity?
hard
Solution
Step 1: Understand Animator blending features
Animator supports layers and transitions that allow smooth blending between animation clips.Step 2: Evaluate other options
Playing clips simultaneously with Animation.Play() or multiple Animation components causes conflicts; manual transform changes are complex and error-prone.Final Answer:
Use Animator layers with weight blending and transitions between states -> Option AQuick Check:
Animator layers + transitions = smooth blending [OK]
Hint: Use Animator layers and transitions for smooth blending [OK]
Common Mistakes:
- Trying to play multiple clips with Animation.Play() simultaneously
- Using multiple Animation components on one object
- Manually animating transforms instead of using Animator
