Learn how to create an Animator Controller, configure a State Machine, and use all parameter types (Float, Bool, Trigger) to control character animations.
You need to create an animation controller for a simple character with three basic actions: Idle, Run, Jump, and Attack.
Assume the following animation clips are already imported into your project:
Idle— character standing stillRun— character runningJump— character jumpingAttack— character performing an attack
Create the following parameters in your controller:
| Parameter Name | Type | Purpose |
|---|---|---|
Speed |
Float | Movement speed (0 to 10) |
IsGrounded |
Bool | Whether the character is on the ground |
Jump |
Trigger | Initiates a jump |
Attack |
Trigger | Initiates an attack |
Idle→Run: whenSpeed > 0.1Run→Idle: whenSpeed < 0.1
Any State→Jump: conditionJump(Trigger)Jump→Idle: conditionIsGrounded == true
Idle→Attack: conditionAttack(Trigger)Run→Attack: conditionAttack(Trigger)Attack→Idle: after the attack animation finishes (enableHas Exit Time)
- If the character attacks while airborne (
IsGrounded == false), the attack should not interrupt the jump (make theAttacktransition only fromIdleandRun, not fromJump). - Add a slight delay or use
Transition Durationto make the attack feel responsive.
- Create an Animator Controller named
PlayerAnimator. - Add states
Idle,Run,Jump,Attack(by dragging the clips into the Animator window). - Create the parameters (
Speed,IsGrounded,Jump,Attack). - Create all transitions according to the logic above.
- Write a simple C# script that:
-
Stores a reference to the
Animator. -
In
Update(), reads keyboard input:W, S, A, DorHorizontal/Vertical— changesSpeed.- Spacebar — calls
SetTrigger("Jump"). - Left Ctrl or mouse — calls
SetTrigger("Attack").
-
Example code:
-
void Update()
{
float moveInput = Input.GetAxis("Vertical");
float speed = Mathf.Abs(moveInput) * 10f;
animator.SetFloat("Speed", speed);
if (Input.GetButtonDown("Jump"))
animator.SetTrigger("Jump");
if (Input.GetKeyDown(KeyCode.LeftControl))
animator.SetTrigger("Attack");
// For demonstration, hardcode IsGrounded = true (if no physics)
animator.SetBool("IsGrounded", true);
}- Attach the
Animatorcomponent to your character and assign the created controller. - Run the scene and verify:
- Pressing
W/Sswitches between Idle and Run. - Pressing Space plays Jump and returns to Idle.
- Pressing Ctrl plays Attack from Idle or Run and returns back.
- Pressing
- ✅ All states switch correctly.
- ✅ Parameters affect transitions.
- ✅ The
JumpandAttacktriggers fire when called from the script (and do not repeat continuously). - ✅ The
Jump → Idletransition only happens afterIsGrounded == true.
An animated character that responds to keyboard input, switching animations smoothly without errors.