Toggling a Boolean Parameter at Specific Points in an Animation – Unity3D Tutorial

Script is included at the bottom of this page!

This tutorial is part of a series of Unity3D tutorials I am writing as I create my game. If you would like to support my work, click here! This script was inspired by Malbers ‘Unka’ Dragon Asset for Unity3D, which includes a more complicated version of this. Malbers makes amazing assets; you should check them out!

Details

What we need: An existing Animation inside an existing Animator.

Input: The string name of your Animator Bool Parameter.

Output: An Animator Bool Parameter easily toggled at specific points in an Animation.

How this saves you time: Instead of going through the hassle of timing Animations, using Animation Events, etc. you can instead use this script to quickly and easily change when a Boolean is set to True (and back to False) during an Animation.

Summary: This simple script uses two sliders to set when a Bool is set to True and when it is set to False during a specific Animation. It uses the Animation State Machine Behavior feature of Unity3D to do this. The input string is the name of the Animator Bool Parameter you would like to toggle.

Output

In the example above, I show how the I_Am_Biting Animator Bool Parameter is being set during a subset of Attack Bite Front‘s total Animation play time. This subset is determined using the two sliders for On and Off (On is when the Bool should be set to True, Off is when it should be set to False).

How to Implement

This method makes use of State Machine Behaviors.

Open the Animator for the character in question, and create (or use an existing) Animator Bool Parameter.

It should be the object with the following component attached:

You should be viewing the following Animator Window:

Click on the Animation that you would like to implement this feature with. For me it was Attack Bite Front.

In the inspector, you will see an option to Add Behavior:

(Attack Flag shown above is what I am calling this script in my project, but the inputs should be the same).

On: When the I_Am_Biting boolean is set to True.

Off: When the I_Am_Biting boolean is set to False.

As you can see with how I set this up on my character, I wanted this boolean to be set to true during a very specific point within this characters bite attack. In my case, it made it so that damage and particle effects wouldn’t trigger until the bite ‘Connected’ with an enemy. You can slide On all the way to the left to have this bool trigger as soon as the Animation plays, or slide Off all the way to the right to have this bool set to false only at the very end of the Animation. As you can see, this can be very useful for many things.

AttackTrigger Behavior Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/* Purpose: This script sets an Animator Bool Parameter to True or False using the current relative time of the Animation itself.
			Useful for situations where a flag needs to be set during a specific time when an animation is played.
   Input: A string representing the name of the Animator Bool Parameter, an 'On' time, and an 'Off' time.
*/

public class AttackFlag : StateMachineBehaviour
{
        public string flagToToggle = ""; // The text name of the Animator Bool Parameter
        [Range(0, 1)] public float On = 0.3f; // When the Bool specified by flagToToggle should be TRUE relative to the animation play time
        [Range(0, 1)] public float Off = 0.6f; // When the Bool specified by flagToToggle should be FALSE relative to the animation play time
		bool isOn, isOff;
		
        override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
        {
            isOn = isOff = false;
		}
		
        override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
        {

            if (!isOn && (stateInfo.normalizedTime % 1) >= On)
            {
                animator.SetBool(flagToToggle, true);
                isOn = true;
            }

            if (!isOff && (stateInfo.normalizedTime % 1) >= Off)
            {
                animator.SetBool(flagToToggle, false);
                isOff = true;
            }
        }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: