CodeBlanks Logo Icon CodeBlanks

Triggering from code

Learn to trigger animations from code with Animator.SetTrigger.

Triggering Animations from Code

The Animator.SetTrigger() method activates a Trigger parameter defined in the Animator Controller from code. This can satisfy the corresponding transition condition and start an animation. The parameter name must match the name in the controller.

animator.SetTrigger("takeDamage");

Controlling States with SetBool

The SetBool() method changes the value of a Bool parameter in the Animator Controller. For example, you can enable a Bool parameter representing a damaged state when a character takes damage. The animation transition uses a condition linked to this parameter.

animator.SetBool("isDamaged", true);

Practice Control the Damage Animation

Set the isDamaged Bool parameter to true in the TakeDamage method. Then set it to false in the ClearDamage method. This allows you to control the character's damaged state through code.

Starter code

Player.cs
using UnityEngine;

public class DamageAnimation : MonoBehaviour
{
    public Animator animator;

    // Hasar ve temizleme metotlarını buraya ekle
}

Sample solution

Player.cs
using UnityEngine;

public class DamageAnimation : MonoBehaviour
{
    public Animator animator;

    public void TakeDamage()
    {
        animator.SetBool("isDamaged", true);
    }

    public void ClearDamage()
    {
        animator.SetBool("isDamaged", false);
    }
}

Expected output

Hasar durumu etkinleştirildi
Hasar durumu temizlendi

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which method resets a Trigger parameter to prevent the animation event from being triggered again?

  • ResetTrigger() Correct answer
  • SetFloat()
  • GetBool()
  • PlayClip()

Explanation: ResetTrigger() resets the specified Trigger parameter.

2 Reset a Trigger

Question
animator.____("takeDamage");
Answer
animator.ResetTrigger("takeDamage");

Explanation: ResetTrigger("takeDamage") resets the Trigger parameter named takeDamage.

3 Match Animation Control from Code

Term Meaning
SetTrigger() Activates a Trigger parameter
ResetTrigger() Resets a Trigger parameter
SetBool() Changes value of a Bool parameter
Animator değişkeni Provides access to Animator component

All lessons in Intermediate