0
0
Unityframework~7 mins

Animator controller in Unity

Choose your learning style9 modes available
Introduction

An Animator controller helps you control character or object animations in Unity. It decides which animation plays and when.

You want a character to walk, run, or jump based on player input.
You want an object to change animations smoothly, like opening and closing a door.
You want to blend between different animations, like blending from idle to walking.
You want to organize many animations and control transitions easily.
You want to trigger animations from code or events.
Syntax
Unity
Animator animator = GetComponent<Animator>();
animator.SetTrigger("Jump");
animator.SetBool("IsRunning", true);

Animator is a component attached to your GameObject.

Use parameters like triggers, bools, floats, or ints to control animation states.

Examples
This triggers the 'Jump' animation once.
Unity
Animator animator = GetComponent<Animator>();
animator.SetTrigger("Jump");
This sets a boolean parameter to control running animation.
Unity
Animator animator = GetComponent<Animator>();
animator.SetBool("IsRunning", true);
This sets a float parameter to control animation blending based on speed.
Unity
Animator animator = GetComponent<Animator>();
float speed = 5.0f;
animator.SetFloat("Speed", speed);
Sample Program

This script gets the Animator component and controls animations based on keyboard input. Pressing space triggers a jump animation. Holding left shift sets running animation.

Unity
using UnityEngine;

public class SimpleAnimatorController : MonoBehaviour
{
    private Animator animator;

    void Start()
    {
        animator = GetComponent<Animator>();
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            animator.SetTrigger("Jump");
            Debug.Log("Jump triggered");
        }

        bool isRunning = Input.GetKey(KeyCode.LeftShift);
        animator.SetBool("IsRunning", isRunning);
        Debug.Log($"IsRunning set to {isRunning}");
    }
}
OutputSuccess
Important Notes

Make sure your Animator controller has parameters matching the names you use in code.

Transitions between animations happen based on conditions you set in the Animator window.

You can preview animations and transitions in Unity's Animator window to understand flow.

Summary

Animator controller manages which animation plays and when.

Use parameters like triggers and bools to control animations from code.

Animator component must be attached to the GameObject you want to animate.