Unity Animation System: Keyframes and Animator
What is Unity Animation?
Unity's animation system is a powerful tool that brings your games to life. It allows you to create smooth, professional animations for characters, objects, and UI elements without writing complex code.
Key Components:
- Animation Clips - Individual animation sequences
- Animator Controller - Manages multiple animations and transitions
- Animator Component - Attached to GameObjects to play animations
- Keyframes - Specific points in time that define animation values
Why Use Unity Animation?
Unity's animation system offers several advantages:
- Visual Editor - Create animations without coding
- State Machine - Organize complex animation logic
- Blending - Smooth transitions between animations
- Performance - Optimized for real-time playback
- Cross-Platform - Works on all Unity-supported platforms
Animation vs Animator
Understanding the difference is crucial:
- Animation - A single animation sequence (like "walk" or "jump")
- Animator - Manages multiple animations and controls when to play them
Think of it like this:
- Animation = A single dance move
- Animator = The choreographer who decides which dance move to perform
Getting Started
Step 1: Create an Animation Clip
- Select a GameObject in your scene
- Open the Animation window (Window > Animation > Animation)
- Click "Create" to make a new Animation Clip
- Name it something descriptive like "PlayerWalk"
Step 2: Record Keyframes
- Click the red "Record" button in the Animation window
- Move the GameObject to a new position
- Unity automatically creates a keyframe
- Move the timeline and repeat to create more keyframes
Step 3: Create an Animator Controller
- Right-click in the Project window
- Create > Animator Controller
- Name it "PlayerAnimator"
- Double-click to open the Animator window
Understanding Keyframes
Keyframes are the foundation of animation. They define specific values at specific times.
Example: Moving a Cube
- Frame 0: Position (0, 0, 0)
- Frame 30: Position (5, 0, 0)
- Frame 60: Position (0, 0, 0)
Unity automatically creates smooth motion between keyframes.
Animation Curves
Animation curves control how values change over time:
- Linear - Constant speed
- Ease In - Starts slow, speeds up
- Ease Out - Starts fast, slows down
- Ease In Out - Slow start and end, fast middle
Pro Tips for Better Animations
1. Use Animation Events
Add code that triggers during animation:
public void OnFootstep()
{
// Play footstep sound
audioSource.Play();
}
2. Optimize Performance
- Use fewer keyframes when possible
- Avoid animating every frame
- Use animation compression for mobile
3. Create Smooth Transitions
- Overlap animation timing slightly
- Use animation curves for natural motion
- Test on target devices for performance
Common Animation Types
Character Animation
- Idle - Standing still
- Walk/Run - Movement animations
- Jump - Vertical movement
- Attack - Combat actions
Object Animation
- Rotation - Spinning objects
- Scale - Growing/shrinking
- Position - Moving objects
- Color - Changing materials
UI Animation
- Fade In/Out - Smooth transitions
- Slide - Moving panels
- Scale - Button presses
- Color - Hover effects
Troubleshooting Common Issues
Animation Not Playing
- Check if Animator is attached
- Verify Animation Clip is assigned
- Ensure GameObject is active
Jerky Animation
- Increase frame rate in Animation settings
- Check for conflicting scripts
- Verify animation length
Performance Problems
- Reduce keyframe count
- Use animation compression
- Avoid animating too many objects
Next Steps
Now that you understand the basics:
- Practice - Create simple animations
- Experiment - Try different animation curves
- Learn - Study animation principles
- Build - Create a complete character animation system
Resources
Ready to create amazing animations? Start with simple movements and gradually work your way up to complex character animations!