Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Animation States and Triggers | Fundamentals of Juice
Unity for Beginners

bookAnimation States and Triggers

Glissez pour afficher le menu

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 Animator component, 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 Jump from 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?

question mark

How would you make the player switch from Idle to Jumping only when the script tells it to?

Sélectionnez la réponse correcte

question mark

How do you make the Jump animation automatically return to Idle after it finishes playing once in Unity's Animator Controller?

Sélectionnez la réponse correcte

question mark

How can you allow the Powerup animation to play whether the player is Idle or mid-Jump in Unity's Animator Controller?

Sélectionnez la réponse correcte

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 5. Chapitre 3

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Section 5. Chapitre 3
some-alt