我最近为我的游戏制作了一个剑攻击动画,动画本身是完美的,但是当我进入播放模式并播放动画(通过左键单击)时它只播放一次,当我再次按下左键单击时它什么也没做,我希望它在我拿着剑按下左键时触发动画
代码:
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");
}
}
}
添加一个代码,让你的剑在你的剑动画播放完后回到空闲状态,就像这样:
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;
}
}