Unity: 2D-анимация стрелка запускается только один раз - PullRequest
0 голосов
/ 06 марта 2019

Я хотел, чтобы моя анимация воспроизводилась всякий раз, когда мой персонаж стреляет из пистолета, но в настоящее время анимация запускается только при первом запуске, а не в остальных.У меня сложилось впечатление, что это будет что-то очень простое в коде, но я был здесь и до сих пор не нашел решения.Может кто-нибудь дать мне подсказку, пожалуйста?

Вот часть анимации в коде:

private Animator myAnimator;
private bool isFire;
private string FireAnimHash = "isFire";

void Awake()
{
    spriteRend = GetComponent<SpriteRenderer>();
}

void Start()
{
    myAnimator = GetComponent<Animator>();
    myAnimator.enabled =true;
    myAnimator.SetBool (FireAnimHash ,isFire);
}

private void Update()
{
    AimArmAtMouse();

    if (Input.GetButtonDown("Fire1"))
    {
        isFire = true;
        myAnimator.SetBool (FireAnimHash ,isFire);
    }
}

Ответы [ 3 ]

0 голосов
/ 06 марта 2019

Вам либо нужно установить isFire = false где-то в вашем коде, либо вам нужно использовать Animator.SetTrigger.


Из документации Unity

//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");
        }
    }
}
0 голосов
/ 06 марта 2019

На самом деле, вы используете триггеры, а не bools, при анимации этих типов анимации, которые становятся истинными, а затем ложными. Скопируйте приведенный ниже скрипт и измените тип переменной для запуска из bool на вкладке аниматора в единице. Он должен работать.

 private Animator myAnimator;
 private string FireAnimHash = "isFire";

 void Awake()
 {
     spriteRend = GetComponent<SpriteRenderer>();
 }

 void Start()
 {
    myAnimator = GetComponent<Animator>();
    myAnimator.enabled = true;
 }

 private void Update()
 {
     AimArmAtMouse();

     if (Input.GetButtonDown("Fire1"))
     {
       myAnimator.SetTrigger(FireAnimHash);
     }
 }
0 голосов
/ 06 марта 2019

Я не знаю, установлен ли ваш isFire в ложь после воспроизведения анимации стрельбы. Если это так, и вы не перенесли его в другие анимации в механике и не задали для цикла анимацию.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...