我试着去让玩家拍摄脚本,并使其重新加载每次弹药小于1,我需要有一个等待,只要它不然后再次让玩家拍摄,但我不知道怎么办。我试图具有可变重载= 1每次弹药小于1,然后有一定量的秒后重装变回0。我知道该怎么做,所以你不能拍,如果重装等于1,但我无法弄清楚如何添加等待。
它可能是最好的,如果你重新写使用协程的代码,
void Start()
{
StartCoroutine(ShootRoutine);
}
IEnumerator ShootRoutine()
{
while (true) // newer exit this loop
{
if (!Input.GetButton ("Fire1")) yield return null; //not shooting, do nothing
else
{
if (ammo>0)
{
Shoot(); // move your ammo decrease here
yield return new WaitForSeconds(delayBetweenShots); // cooldown after shot
}
if (ammo<=0)
{
DisableEffects();
Reload();
yield return new WaitForSeconds(reloadTime);
EnableEffects();
}
}
} // while(true) loops back here
}
同时,没有必要定时器+ = Time.deltaTime - 此值始终为可用了time.time
在你重装的方法,就可以启动转向reload
变回原来的值之前等待的秒数协同程序。
private IEnumerator WaitForFewSeconds()
{
yield return new WaitForSeconds(2);
reload = 0;
}
void Reload()
{
// do your thing
StartCoroutine(WaitForFewSeconds());
}