unity3d - Unity 2D:如何使用 deltaTime 在 while 循环中减少静态 int
问题描述
在我的 Player 脚本中,我设置了一个名为 boost 的静态 int 以用于任何脚本。
在整个游戏过程中,我有一些助推器拾取硬币,当玩家得到一个时,它会为他们的助推器罐加满 +1
您需要最少 3 个助推币才能开始在车辆中助推。当玩家使用助推器时,助推器将持续大约与收集的助推器硬币一样多的秒数。使用增压应减少增压箱。
播放器脚本
public static int boost;
private void OnTriggerEnter2D(Collider2D otherObject)
{
if (otherObject.tag == "Boost")
{
boost++;
GetComponent<AudioSource>().PlaySound(BoostSound, 5.7F);
Destroy(otherObject.gameObject);
}
}
按钮调用代码
public void BoostButton()
{
StartCoroutine("Use_VehicleBoost");
}
按钮调用的助推器代码。
IEnumerator Use_VehicleBoost()
{
// Check which boost package player picked
int boostLevel = SecurePlayerPrefs.GetInt("BoostLevel");
if (boostLevel == 0)
{
Debug.Log("Boost Level: None");
yield return null;
}
else if (boostLevel == 1)
{
Debug.Log("Boost Level: 1");
float aceleRate = 400, maxFWD = -2500;
while (Player.boost >= 3)
{
vehicleController.GetComponent<CarMovement>().accelerationRate += aceleRate;
vehicleController.GetComponent<CarMovement>().maxFwdSpeed += maxFWD;
// Meant to slowly take one point/ Time second away from boost tank // Problem is here ----->>>
Player.boost = Player.boost - Mathf.RoundToInt(Time.deltaTime);
yield return null;
}
if (Player.boost <= 0)
{
yield return null;
}
}
yield return null;
}
问题就在这一行
Player.boost = Player.boost - Mathf.RoundToInt(Time.deltaTime);
它应该以秒为单位递减Player.boost
。例如,如果玩家收集了 3 个提升币,那么提升在使用时会处于活动状态,它将持续 3 秒然后关闭。
不完全确定在这里做什么。他们告诉我,在 while 循环中,deltaTime
因为它停留在一帧中,所以它保持在值 0?我应该启动一个计时器变量吗?谢谢你。
解决方案
不完全确定在这里做什么。他们告诉我,在 while 循环中,deltaTime 保持在值 0,因为它停留在一帧中?我应该启动一个计时器变量吗?
是的,我确实说过,但是您已经解决了yield return null;
让循环等待一帧的一帧问题,因此Time.deltaTime
有机会进行更改。即使解决了这个问题,我告诉你的仍然是 0 个问题,但因为while (Player.boost >= 3)
仍然是正确的。这是真的,因为Mathf.RoundToInt(Time.deltaTime);
它返回零。您可以使用 来验证这一点Debug.Log(Mathf.RoundToInt(Time.deltaTime))
。
如果Player.boost
type 是 an int
,则将其更改为float
type ,以Time.deltaTime
直接逐渐减少它:
while (Player.boost >= 3)
{
vehicleController.GetComponent<CarMovement>().accelerationRate += aceleRate;
vehicleController.GetComponent<CarMovement>().maxFwdSpeed += maxFWD;
// Meant to slowly take one point/ Time second away from boost tank
Player.boost = Player.boost - Time.deltaTime;
//OR Player.boost -= Time.deltaTime;
yield return null;
}
如果Player.boost
type 是 anint
但您不想将其更改为float
,请删除Time.deltaTime
用于float
值的 ,然后使用 进行等待WaitForSeconds
。有了这个,您可以在每次WaitForSeconds
通话后从 boost 中减去一个。
while (Player.boost >= 3)
{
vehicleController.GetComponent<CarMovement>().accelerationRate += aceleRate;
vehicleController.GetComponent<CarMovement>().maxFwdSpeed += maxFWD;
// Meant to slowly take one point/ Time second away from boost tank
Player.boost = Player.boost - 1;
//OR Player.boost -= 1;
yield return new WaitForSeconds(1f);
}
推荐阅读
- c# - 删除父项时删除子项
- html - 中心绝对值 UL's LI
- java - 在 WebView 中选择元素并获取详细信息
- java - 如何用 JPanel 填充 JFrame 窗口?
- firebase - Firebase 动态链接在 gmail 中被标记为可疑
- python - 如何从未在其中定义“def main”的 Python 文件中导入 main 函数?
- swift - UIPageViewControllerDelegate:'NSInternalInconsistencyException',原因:'无效参数不满足:[views count] == 3'
- c# - 如何打印到下一页溢出的字符串?
- docker - Hyperledger - Docker swarm 在部署到多个主机时失败
- php - 将 php 脚本添加到网站登录/注册功能时出现 HTTP ERROR 500