版本: Unity 6 (6000.0)
语言英语
  • C#

Animator.SetBool

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交的内容,但我们会阅读用户提出的每个建议的更改,并在适用的情况下进行更新。

关闭

提交失败

由于某种原因,您的建议更改无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

切换到手册

声明

public void SetBool(string name, bool value);

声明

public void SetBool(int id, bool value);

参数

name 参数名称。
id 参数 ID。
value 新的参数值。

描述

设置给定布尔参数的值。

使用 Animator.SetBool 通过脚本将布尔值传递到 动画控制器

使用此方法触发动画状态之间的转换。例如,通过将“alive”布尔值设置为 false 来触发死亡动画。有关设置动画器的更多信息,请参阅有关 动画 的文档。

注意:您可以通过名称或 ID 号识别参数,但名称或 ID 号必须与您要更改的动画器中的参数相同。

//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 mouse click or a tap of the screen.

using UnityEngine;

public class Example : 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() { //Click the mouse or tap the screen to change the animation if (Input.GetMouseButtonDown(0)) 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); } }