Unity 6.3
0 онлайн 67 гостей 3 в системе
Вход

Animator.SetTrigger

Declaration

public void SetTrigger(string name);
public void SetTrigger(int id);

Параметры

Параметр Описание
имя Имя параметра.
Идентификатор Параметр ID.

Описание

Устанавливает значение заданного параметра триггера.

Этот метод позволяет установить (то есть активировать) триггер анимации, чтобы вызвать смену потока в состояниях Animator Controller. Animation Параметры страница описывает цель Animator Окно Параметры контроллера. Trigger является одним из 4-х выбираемых вариантов. Выбор этого варианта добавляет Trigger в список выбранных параметров. После добавления в выбранный список параметр может быть назван. В отличие от boolс, имеющими одинаковые true/false вариант, Triggerс имеют true опция, которая автоматически возвращается обратно к false. Типичный пример — параметр Jump. Если этот параметр задан во время выполнения, персонаж прыгает. По завершении прыжка возвращается предыдущее состояние движения (например, ходьба или бег).

В приведенном ниже примере нажатие 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"); } } }