【发布时间】:2017-02-24 20:11:28
【问题描述】:
我有两个函数,我希望在 Update() 函数中每 5 秒调用一次harmPlayer()。但是它从 update() 函数中执行了数百次。我知道 Update() 函数在每一帧上都被执行,并且每次都会调用harmPlayer(),那么我该如何实现等待 5 秒'
IEnumerator HarmPlayer()
{
Debug.Log("Inside Harm Player");
yield return new WaitForSeconds(5);
Debug.Log("Player Health is the issue");
}
这是我的 Update() 函数
void Update () {
transform.LookAt(target);
float step = speed * Time.deltaTime;
distance = (transform.position - target.position).magnitude;
if (distance < 3.5)
{
animator.SetFloat("Attack", 0.2f);
StartCoroutine("HarmPlayer");
}
}
【问题讨论】:
-
那是因为你的协程在每次更新时都会被调用,等待 5 秒,然后在所有被调用的时候处理协程。尝试使用 InvokeRepeating