0
0
UnityHow-ToBeginner ยท 4 min read

How to Create Animation in Unity: Step-by-Step Guide

To create animation in Unity, use the Animator component with an Animator Controller to manage animation states. You create animations by recording keyframes in the Animation window and then control them via scripts or the Animator.
๐Ÿ“

Syntax

Unity animations are created using the Animation and Animator components. The Animation component plays simple animations, while the Animator uses an Animator Controller to manage complex state machines.

Key parts:

  • AnimationClip: The actual animation data (keyframes).
  • Animator Controller: Controls which animation plays and when.
  • Animator component: Attached to GameObject to run animations.
csharp
public class SimpleAnimation : MonoBehaviour {
    public Animator animator;

    void Start() {
        animator.Play("Run");
    }
}
Output
The GameObject plays the 'Run' animation when the scene starts.
๐Ÿ’ป

Example

This example shows how to create a simple animation clip, assign it to an Animator Controller, and trigger it from a script.

csharp
using UnityEngine;

public class AnimationExample : MonoBehaviour {
    public Animator animator;

    void Update() {
        if (Input.GetKeyDown(KeyCode.Space)) {
            animator.SetTrigger("Jump");
        }
    }
}
Output
When the spacebar is pressed, the GameObject plays the 'Jump' animation defined in the Animator Controller.
โš ๏ธ

Common Pitfalls

Common mistakes when creating animations in Unity include:

  • Not assigning the Animator Controller to the Animator component.
  • Forgetting to create and set animation parameters (like triggers or bools) in the Animator Controller.
  • Trying to play animations without properly naming them or mismatching names in code.
  • Using the Animation component instead of Animator for complex animations.
csharp
/* Wrong way: Trying to play animation without Animator Controller */
public class WrongAnimation : MonoBehaviour {
    public Animator animator;

    void Start() {
        animator.Play("Run"); // Won't work if Animator Controller is missing
    }
}

/* Right way: Assign Animator Controller and use parameters */
public class RightAnimation : MonoBehaviour {
    public Animator animator;

    void Start() {
        animator.SetTrigger("RunTrigger");
    }
}
๐Ÿ“Š

Quick Reference

Tips for creating animations in Unity:

  • Use the Animation window to record keyframes for your GameObject.
  • Create an Animator Controller to manage animation states and transitions.
  • Attach an Animator component to your GameObject and assign the Animator Controller.
  • Control animations via script using animator.SetTrigger(), animator.Play(), or other parameter methods.
  • Test animations in Play mode and adjust transitions for smoothness.
โœ…

Key Takeaways

Use the Animator component with an Animator Controller to create and manage animations.
Create Animation Clips by recording keyframes in the Animation window.
Control animations in scripts using triggers and parameters on the Animator.
Always assign the Animator Controller to the Animator component to avoid errors.
Test animations in Play mode and refine transitions for smooth results.