我想让我的脚本检查玩家在给定的y坐标下有多长时间。但是,因为我要检查FixedUpdate空中的信息,所以不能直接添加while循环。因此,我尝试了以下方法:
void FixedUpdate()
{
if(rb.position.y < 1f)
{
checkIfLost();
}
}
IEnumerator checkIfLost()
{
while(rb.position.y < 1f)
{
float timeGiven = 5 - Time.deltaTime;
if(timeGiven <= 0)
{
FindObjectOfType<GameManager>().EndGame();
}
yield return null;
}
}这不起作用。我是unity C#的新手,我试着在网上搜索它,但没有找到任何东西。
运行while循环并检查玩家在y坐标下有多长时间,有什么更好的替代方案?
发布于 2020-05-25 01:57:05
只需将一个浮点变量设置为计数器
private float timer = 0f;
void FixedUpdate()
{
if(rb.position.y < 1f)
{
timer +=Time.fixedDeltaTime;
if (timer > 5f)
{
//do something
FindObjectOfType<GameManager>().EndGame();
}
}
else
{
timer = 0f;
}
}如果你想在更新中做到这一点(更好),只需替换timer +=Time.DeltaTime;
https://stackoverflow.com/questions/61989797
复制相似问题