首页 > 解决方案 > 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?我应该启动一个计时器变量吗?谢谢你。

标签: unity3d

解决方案


不完全确定在这里做什么。他们告诉我,在 while 循环中,deltaTime 保持在值 0,因为它停留在一帧中?我应该启动一个计时器变量吗?

是的,我确实说过,但是您已经解决了yield return null;让循环等待一帧的一帧问题,因此Time.deltaTime 有机会进行更改。即使解决了这个问题,我告诉你的仍然是 0 个问题,但因为while (Player.boost >= 3)仍然是正确的。这是真的,因为Mathf.RoundToInt(Time.deltaTime);它返回零。您可以使用 来验证这一点Debug.Log(Mathf.RoundToInt(Time.deltaTime))

如果Player.boosttype 是 an int,则将其更改为floattype ,以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.boosttype 是 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);
}

推荐阅读