我正在制作Unity剑游戏。我有它,所以当您单击Mouse0时,它将播放动画并将攻击更改为True。我正在尝试使其在两秒钟后(或在动画切换为“空闲”之后)将攻击更改为“假”。我拥有的代码使Unity停止响应。
编辑:我知道代码处于无限循环中,但是我需要它,所以当我单击mouse0时,它就会起作用,而不仅仅是在测试开始时。有没有一种解决方法/更好的方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwordSwing : MonoBehaviour
{
Animator anim;
public static bool attack = false;
private bool a = true;
void Start()
{
StartCoroutine(waiter());
anim = gameObject.GetComponent<Animator>();
}
IEnumerator waiter()
{
while(a = true)
if (Input.GetMouseButtonDown(0))
{
Debug.Log("True");
attack = true;
anim.SetTrigger("Active");
anim.SetTrigger("Idle");
yield return new WaitForSeconds(2);
attack = false;
Debug.Log("False");
}
}
}
答案 0 :(得分:0)
在yield return null;
的末尾添加一个while
,以便它可以从协程返回并完成帧。
另外,用while(a == true)
(用双=
)或仅用while(a)
代替while条件:
while(a == true) {
if (Input.GetMouseButtonDown(0)){
Debug.Log("True");
attack = true;
anim.SetTrigger("Active");
anim.SetTrigger("Idle");
yield return new WaitForSeconds(2);
attack = false;
Debug.Log("False");
}
yield return null;
}