Loading...
Loading...
Drive Unity 6 character animation with Animator Controllers: states, transitions, parameters, blend trees, animation layers, and humanoid Avatar IK. Use when wiring an Animator, setting parameters from script (SetFloat/SetBool/SetTrigger), building blend trees, or when the user mentions Animator, Mecanim, state machine, blend tree, or .controller.
npx skill4agent add gamedev-skills/awesome-gamedev-agent-skills unity-animationAnimator*.controller*.animunity-csharp-scriptingAnimatorFloatBoolIntTriggerSetFloatSetBoolSetIntegerSetTriggerFloatlayerWeightusing UnityEngine;
[RequireComponent(typeof(Animator))]
public class CharacterAnim : MonoBehaviour
{
private Animator _anim;
// Cache parameter hashes — faster and typo-proof vs string lookups every frame.
private static readonly int Speed = Animator.StringToHash("Speed");
private static readonly int IsGrounded= Animator.StringToHash("IsGrounded");
private static readonly int Jump = Animator.StringToHash("Jump");
private void Awake() => _anim = GetComponent<Animator>();
public void Tick(float planarSpeed, bool grounded)
{
_anim.SetFloat(Speed, planarSpeed); // drives a 1D blend tree (idle/walk/run)
_anim.SetBool(IsGrounded, grounded); // gates a falling/landing transition
}
public void DoJump() => _anim.SetTrigger(Jump); // fire-and-forget; auto-resets after use
}// dampTime smooths Speed so the blend tree doesn't snap; great for analog sticks.
_anim.SetFloat(Speed, targetSpeed, 0.1f /* dampTime */, Time.deltaTime);// Useful for hit reactions where you want an immediate, explicit transition.
_anim.CrossFade("Hit", 0.1f); // blend over 0.1s normalized
// Or jump instantly: _anim.Play("Hit");private System.Collections.IEnumerator AfterAttack()
{
var info = _anim.GetCurrentAnimatorStateInfo(0); // layer 0
yield return new WaitForSeconds(info.length); // approximate clip length
// ...follow-up logic
}SetTriggerResetTriggerBoolAnimator.StringToHashHas Exit TimeApply Root MotionlayerWeightOnAnimatorIKOnAnimatorIKSetIKPositionWeightSetIKPositionreferences/blend-trees-and-ik.mdScriptReference/Animatorunity-csharp-scriptingunity-physicsgame-ai