Animation States and Triggers
Свайпніть щоб показати меню
Animation Controller: Switching States & Adding Bounce Animation
The player already has an Idle animation - but when it bounces off a platform, it should look like it's jumping.
Creating the Jump Animation
Once you've got the jump frames ready, create the animation by dragging the frames into the Scene. Save the animation (e.g. Character_Jumping) and delete it from the scene (we want it controlled by the Player object).
Using the Animator Window
- select the Player
GameObject; - in the Inspector, within its
Animatorcomponent, click on its Controller to highlight its Animator Controller in the Project tab; - double-click the Animation Controller to open the Animator tab.
You'll see the current animation state: Idle.
To add jumping:
- drag the jumping animation (
Character_Jumping) from the Project window into the Animator window.
Creating Transitions Between States
We want:
- Idle -> Jumping;
- Jumping -> Idle.
To create transitions:
- right-click Idle;
- select Make Transition;
- click Jumping.
Then repeat in reverse (Jumping -> Idle).
Setting Transition Conditions
Idle -> Jumping (Trigger-based)
First, we need to create the Trigger as a parameter inside the Animator.
Step 1: Create the Trigger Parameter
In the Animator window:
- open the Parameters tab (next to Layers);
- click the + button;
- select Trigger;
- name it
Jump.
Now the Animator knows that a trigger called Jump exists.
Step 2: Add It as a Transition Condition
Now click the arrow from Idle -> Jumping.
In the Inspector:
- click Add Condition;
- select
Jumpfrom the dropdown list.
This means the jump animation will only play when triggered by code.
Jumping -> Idle (Automatic Return)
Click the arrow from Jumping to Idle.
- enable Has Exit Time;
- set Exit Time to
1.
This means:
The Jump animation must finish playing once before returning to Idle.
You can also reduce Transition Duration if you want a snappier return.
Triggering the Animation from Script
Because the Animator is on the Player object, we'll trigger it inside PlayerController.
Step 1: Reference the Animator
Animator animC;
void Start()
{
animC = GetComponent<Animator>();
}
Step 2: Detect Platform Collision
void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.name.Contains("platform"))
{
animC.SetTrigger("Jump");
}
}
Now when the player hits a platform, the Jump animation plays. The game immediately feels more responsive and alive.
1. How would you make the player switch from Idle to Jumping only when the script tells it to?
2. How do you make the Jump animation automatically return to Idle after it finishes playing once in Unity's Animator Controller?
3. How can you allow the Powerup animation to play whether the player is Idle or mid-Jump in Unity's Animator Controller?
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат