Unity:Shooter 2D角色动画仅运行一次

时间:2019-03-06 11:25:59

标签: c# unity3d animation

我希望我的角色每次射击时都可以播放动画,但目前该动画仅在第一次触发时才运行,而在其他情况下不运行。我的印象是代码中的内容将非常简单,但我一直在这里,但仍未找到解决方案。有人可以给我一个提示吗?

这是代码中的动画部分:

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 :(得分:0)

I don't know if your isFire is set to false after playing the shooting animation. If it has been true, and you have not transitioned it to other animations in the mecanim and did not set the animation to loop.

答案 1 :(得分:0)

您要么需要在代码中的某个位置设置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");
        }
    }
}

答案 2 :(得分:0)

实际上,在对这些类型的动画进行动画处理时,您会使用触发器而不是布尔控件,这些动画会先变为true,然后变为false。 复制以下脚本,并在统一的动画器选项卡中将变量类型更改为从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);
     }
 }