Legacy Documentation: Version 2017.2 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Animator.SetBool

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public method SetBool(name: string, value: bool): void;
public void SetBool(string name, bool value);
public method SetBool(id: int, value: bool): void;
public void SetBool(int id, bool value);

Description

Sets an Animator bool parameter.

Use Animator.SetBool to pass Boolean values to an Animator Controller via script.

Use this to trigger transitions between Animator states. For example, triggering a death animation by setting an “alive” boolean to false. See documentation on Animation for more information on setting up Animators.

Note: You can identify the parameter by name or by ID number, but the name or ID number must be the same as the parameter you want to change in the Animator.

no example available in JavaScript
//Set up a new Boolean parameter in the Unity Animator and name it, in this case “Jump”.
//Set up transitions between each state that the animation could follow. For example, the player could be running or idle before they jump, so both would need transitions into the animation.
//If the “Jump” boolean is set to true at any point, the m_Animator plays the animation. However, if it is ever set to false, the animation would return to the appropriate state (“Idle”).
//This script enables and disables this boolean in this case by listening for the press of the space key.

using UnityEngine;

public class AnimationSetBool : MonoBehaviour { //Fetch the Animator Animator m_Animator; // Use this for deciding if the GameObject can jump or not bool m_Jump;

void Start() { //This gets the Animator, which should be attached to the GameObject you are intending to animate. m_Animator = gameObject.GetComponent<Animator>(); // The GameObject cannot jump m_Jump = false; }

void Update() { //If the Space button is pressed, the GameObject can jump if (Input.GetKey(KeyCode.Space)) m_Jump = true;

//Otherwise the GameObject cannot jump. else m_Jump = false;

//If the GameObject is not jumping, send that the Boolean “Jump” is false to the Animator. The jump animation does not play. if (m_Jump == false) m_Animator.SetBool("Jump", false);

//The GameObject is jumping, so send the Boolean as enabled to the Animator. The jump animation plays. if (m_Jump == true) m_Animator.SetBool("Jump", true); } }
对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答