Animation & navigation¶
Drive a character's Animator, send an NPC walking with a NavMeshAgent, and (optionally) steer limbs with IK. These wrap Unity's components directly.
Animator¶
Get the Animator off an object, then set its parameters — exactly the parameter names you defined in the Animator Controller asset.
Animator anim = GetComponent<Animator>();
anim.SetFloat("Speed", velocity.magnitude); // blend trees
anim.SetBool("Grounded", grounded);
anim.SetTrigger("Jump"); // one-shot transition
anim.SetInteger("WeaponType", 2);
| Set a parameter | |
|---|---|
SetFloat(name, v) · SetBool(name, b) · SetInteger(name, i) |
continuous / flag / enum params |
SetTrigger(name) · ResetTrigger(name) |
fire / clear a trigger |
GetFloat / GetBool / GetInteger(name) |
read a parameter back |
Play or crossfade a state directly (bypassing transitions):
anim.Play("Attack"); // snap to a state
anim.CrossFade("Run", 0.2f); // blend over 0.2s
anim.speed = 1.5f; // play faster (also: enabled, applyRootMotion)
Read what's playing via AnimatorStateInfo:
AnimatorStateInfo st = anim.GetCurrentAnimatorStateInfo(0); // layer 0
if (st.IsName("Attack") && st.normalizedTime >= 1f)
anim.SetTrigger("Idle"); // attack finished → go idle
bool blending = anim.IsInTransition(0);
AnimatorStateInfo: IsName · IsTag · normalizedTime (0..1 through the clip) · length · speed · loop.
Also available: layers (GetLayerWeight / SetLayerWeight, GetLayerName, layerCount), root motion (applyRootMotion, deltaPosition, rootPosition), and a rich IK set (SetIKPosition / SetIKPositionWeight, SetIKRotation*, SetLookAtPosition / SetLookAtWeight, GetBoneTransform) for hand/foot/look targeting.
Animation is visual — keep gameplay separate
Animator state is cosmetic and runs per‑client. Don't gate gameplay logic on normalizedTime alone if the outcome must match across players — decide the outcome in script/server logic and use the Animator to show it.
NavMeshAgent — walk an NPC somewhere¶
If your NPC (or any object) has a NavMeshAgent and the world is baked with a NavMesh, you can send it to a point and it pathfinds around obstacles:
NavMeshAgent agent = GetComponent<NavMeshAgent>();
agent.SetDestination(target.position); // start walking there
agent.speed = 3.5f;
protected override void Update()
{
if (agent.hasPath && agent.remainingDistance < 0.5f)
Debug.Log("Arrived");
if (alarmed) agent.isStopped = true; // halt without losing the path
}
agent.ResetPath(); // cancel and clear the path
SetDestination(point) |
path to a world position |
ResetPath() · isStopped |
cancel / pause |
speed · velocity |
movement tuning & current motion |
hasPath · remainingDistance |
"am I going somewhere / how far left" |
Pair NavMesh movement with an Animator
A NavMeshAgent moves the transform; it doesn't animate. Feed agent.velocity.magnitude into anim.SetFloat("Speed", …) so the walk/run animation matches the motion.
IK¶
IK.EnableScriptControl() opts an Animator into script‑driven inverse kinematics, after which the Animator's SetIK* / SetLookAt* calls take effect (call them from a LateUpdate/IK pass). Use for grabbing, foot placement, or making an NPC look at a player:
IK.EnableScriptControl();
anim.SetLookAtWeight(1f);
anim.SetLookAtPosition(Player.GetHeadPosition(speakerId));
Quick reference¶
| Group | Calls |
|---|---|
| Params | SetFloat · SetBool · SetInteger · SetTrigger · ResetTrigger · GetFloat/Bool/Integer |
| Playback | Play · CrossFade · CrossFadeInFixedTime · speed · enabled |
| State | GetCurrentAnimatorStateInfo · GetNextAnimatorStateInfo · IsInTransition · AnimatorStateInfo.IsName/normalizedTime |
| Layers / IK | GetLayerWeight · SetLayerWeight · SetIKPosition · SetLookAtPosition · GetBoneTransform · IK.EnableScriptControl |
| NavMesh | SetDestination · ResetPath · isStopped · speed · velocity · hasPath · remainingDistance |
→ Combine with NPC & Self for AI characters and Math & vectors for the positions these calls take.