我的动画在播放模式下播放一次后无法播放

问题描述 投票:0回答:1

我最近为我的游戏制作了一个剑攻击动画,动画本身是完美的,但是当我进入播放模式并播放动画(通过左键单击)时它只播放一次,当我再次按下左键单击时它什么也没做,我希望它在我拿着剑按下左键时触发动画

代码:

public class animationactivation : MonoBehaviour

{

 public GameObject Sword;

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown("mouse 0"))
    {
        Sword.GetComponent<Animator>().Play("swing sword");
    }

   
   

}

}

c# unity3d animation
1个回答
0
投票

添加一个代码,让你的剑在你的剑动画播放完后回到空闲状态,就像这样:

public class animationactivation : MonoBehaviour
{

 public GameObject Sword;
 public bool CanAttack = true;//this is to ready your sword for another attack
 public float AttackCooldown = 1.0f;//this is how much time your sword will go back to idle again
 public 

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown("mouse 0"))
    {
        CanAttack = false;
        Sword.GetComponent<Animator>().Play("swing sword");
        StartCoroutine(ResetAttackCooldown())
    }      
}
//this code activates the cooldown
IEnumerator ResetAttackCooldown()
    {
        yield return new WaitForSeconds(AttackCooldown);
        CanAttack = true;
    }

}

© www.soinside.com 2019 - 2024. All rights reserved.