0
0
Unityframework~5 mins

Animation parameters in Unity

Choose your learning style9 modes available
Introduction

Animation parameters let you control how animations change based on your game actions. They help your character move smoothly and react to what happens.

When you want your character to switch from walking to running based on speed.
When you want to trigger a jump animation when the player presses the jump button.
When you want to blend between different animations like idle and attack.
When you want to control animation flow using game events like taking damage.
When you want to create smooth transitions between animations automatically.
Syntax
Unity
animator.SetBool("isRunning", true);
animator.SetFloat("speed", 3.5f);
animator.SetTrigger("jump");
animator.SetInteger("health", 100);

Use SetBool for true/false values.

Use SetFloat for decimal numbers like speed.

Examples
This sets a boolean parameter named isJumping to true, which can start a jump animation.
Unity
animator.SetBool("isJumping", true);
This sets a float parameter named speed to 5.0, which can control how fast a running animation plays.
Unity
animator.SetFloat("speed", 5.0f);
This triggers an animation event called attack once, like swinging a sword.
Unity
animator.SetTrigger("attack");
This sets an integer parameter named health to 50, which can be used to change animations based on health level.
Unity
animator.SetInteger("health", 50);
Sample Program

This script controls a player character's animations. It updates the speed parameter based on horizontal input, triggers a jump animation when the jump button is pressed, and sets a boolean isRunning to true when the player moves.

Unity
using UnityEngine;

public class PlayerAnimationController : MonoBehaviour
{
    private Animator animator;

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

    void Update()
    {
        float speed = Input.GetAxis("Horizontal");
        animator.SetFloat("speed", Mathf.Abs(speed));

        if (Input.GetButtonDown("Jump"))
        {
            animator.SetTrigger("jump");
        }

        bool isRunning = Mathf.Abs(speed) > 0.1f;
        animator.SetBool("isRunning", isRunning);
    }
}
OutputSuccess
Important Notes

Animation parameters must match exactly the names set in the Animator window.

Use SetTrigger for one-time animation events.

Keep parameter types consistent to avoid errors.

Summary

Animation parameters control how animations change during gameplay.

Use booleans, floats, integers, and triggers to manage animation states.

Update parameters in your script to make animations respond to player actions.