How to Use Animator Parameters in Unity for Smooth Animations
In Unity, use
Animator parameters to control animation states by setting values like bool, float, int, or trigger in your script. Access these parameters via the Animator component using methods like SetBool, SetFloat, SetInteger, and SetTrigger to change animations during gameplay.Syntax
Animator parameters are variables defined in the Animator Controller that you can set from scripts to control animation flow.
Common methods to set parameters in code:
SetBool(string name, bool value): Sets a boolean parameter.SetFloat(string name, float value): Sets a float parameter.SetInteger(string name, int value): Sets an integer parameter.SetTrigger(string name): Activates a trigger parameter.ResetTrigger(string name): Resets a trigger parameter.
You get the Animator component from your GameObject and call these methods with the parameter name defined in the Animator Controller.
csharp
Animator animator = GetComponent<Animator>(); animator.SetBool("isRunning", true); animator.SetFloat("speed", 1.5f); animator.SetInteger("health", 100); animator.SetTrigger("jump"); animator.ResetTrigger("jump");
Example
This example shows how to toggle a walking animation using a boolean parameter called isWalking. When the player presses the W key, the walking animation starts; when released, it stops.
csharp
using UnityEngine; public class PlayerMovement : MonoBehaviour { private Animator animator; void Start() { animator = GetComponent<Animator>(); } void Update() { if (Input.GetKeyDown(KeyCode.W)) { animator.SetBool("isWalking", true); } if (Input.GetKeyUp(KeyCode.W)) { animator.SetBool("isWalking", false); } } }
Output
When pressing W, the character's walking animation plays; releasing W stops it.
Common Pitfalls
Common mistakes when using animator parameters include:
- Using the wrong parameter name string, causing no animation change.
- Not defining the parameter in the Animator Controller before setting it in code.
- Forgetting to reset triggers, which can cause animations to get stuck.
- Setting parameters on the wrong Animator component or GameObject.
Always double-check parameter names and Animator setup.
csharp
/* Wrong: Parameter name typo */ animator.SetBool("isWalkng", true); // Misspelled "isWalking" /* Right: Correct parameter name */ animator.SetBool("isWalking", true); /* Wrong: Trigger not reset, animation may not restart */ animator.SetTrigger("jump"); // Missing animator.ResetTrigger("jump"); /* Right: Reset trigger after use */ animator.SetTrigger("jump"); animator.ResetTrigger("jump");
Quick Reference
| Method | Parameter Type | Description |
|---|---|---|
| SetBool(string name, bool value) | bool | Sets a boolean parameter to true or false. |
| SetFloat(string name, float value) | float | Sets a float parameter to control blend trees or speed. |
| SetInteger(string name, int value) | int | Sets an integer parameter for state control. |
| SetTrigger(string name) | trigger | Activates a trigger parameter to start an animation. |
| ResetTrigger(string name) | trigger | Resets a trigger parameter to allow re-triggering. |
Key Takeaways
Animator parameters control animation states and must be defined in the Animator Controller first.
Use SetBool, SetFloat, SetInteger, and SetTrigger methods on the Animator component to change animations.
Always use exact parameter names as defined in the Animator Controller to avoid errors.
Reset triggers after use to prevent animations from getting stuck.
Get the Animator component from the GameObject before setting parameters.