c# - 如何在 C# 中检查玩家低于 y 坐标的时间
问题描述
我希望我的脚本检查玩家低于给定 y 坐标的时间。但是,由于我正在检查 FixedUpdate void 中的信息,因此无法直接添加 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 坐标下方的时间有什么更好的选择?
解决方案
只需将浮点变量设置为计数器
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;
推荐阅读
- powershell - Powershell 通过 ObjectID 使用 add-azureadgroupmember 将设备添加到组
- html - HTML 按钮溢出
- c# - PDFsharp - 尝试为我刚刚与页面链接合并的所有文件制作目录
- excel - 如何遍历表格并将电子邮件发送给相应的用户
- reactjs - 尝试导入错误:“网格”未从“react-bootstrap”导出
- excel - 如何找到两个日期之间的值?
- python - Django TypeError: __init__() 得到了一个意外的关键字参数 'attrs'
- entity-relationship - 现在不知道
- c# - 如果 ViewBag.Elements 为空,则 MVC 重定向
- apache-kafka - 如何获取Kafka主题的序列化格式?