name | 参数名称。 |
id | 参数 ID。 |
设置给定触发器参数的值。
此方法允许您设置(即激活)动画触发器,以在动画控制器状态机中导致流程发生变化。 “动画参数”页面描述了“动画控制器参数”窗口的用途。 Trigger
是 4 个可选选项之一。选择此选项会将 Trigger
添加到所选参数列表中。添加后,可以对其命名。与具有相同 true/false
选项的 bool
不同,Trigger
具有一个 true
选项,该选项会自动返回到 false
。一个典型的示例可能是拥有一个“跳跃”选项。如果在运行时输入此选项,角色将跳跃。在跳跃结束时,将返回到之前的动作(可能是步行或跑步状态)。
在下面的示例脚本中,按下 UpArrow
或 DownArrow
使用 SetTrigger 激活“跳跃”或“蹲下”触发器。
//Attach this script to a GameObject with an Animator component attached. //For this example, create parameters in the Animator and name them “Crouch” and “Jump” //Apply these parameters to your transitions between states
//This script allows you to trigger an Animator parameter and reset the other that could possibly still be active. Press the up and down arrow keys to do this.
using UnityEngine;
public class Example : MonoBehaviour { Animator m_Animator;
void Start() { //Get the Animator attached to the GameObject you are intending to animate. m_Animator = gameObject.GetComponent<Animator>(); }
void Update() { //Press the up arrow button to reset the trigger and set another one if (Input.GetKey(KeyCode.UpArrow)) { //Reset the "Crouch" trigger m_Animator.ResetTrigger("Crouch");
//Send the message to the Animator to activate the trigger parameter named "Jump" m_Animator.SetTrigger("Jump"); }
if (Input.GetKey(KeyCode.DownArrow)) { //Reset the "Jump" trigger m_Animator.ResetTrigger("Jump");
//Send the message to the Animator to activate the trigger parameter named "Crouch" m_Animator.SetTrigger("Crouch"); } } }