我在做一个统一剑的游戏。我有它,所以当您单击Mouse0时,它会播放一个动画并更改攻击到True。我正试图使它,所以在两秒钟后(或动画切换到空闲),它改变攻击为假。我的代码使团结停止响应。
编辑:我知道代码处于无限循环中,但我需要它,所以当我单击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");
}
}
}发布于 2020-01-11 00:24:37
在yield return null;的末尾添加一个while,这样它就可以从Coroutine返回并完成框架。
另外,用while(a == true) (使用双=)或仅用while(a)替换with条件
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;
}https://stackoverflow.com/questions/59690542
复制相似问题