随着时间的推移在两个值之间变小

我正在尝试通过时间值减少浮点数,我正在使用Unity并停止时间,Time.timeScale = 0f;所以不能Time.deltaTime在while循环中使用'Time.realtimeSinceStartup'来使用,因此我从全局脚本中读取了主音量变量,玩家可以在游戏中设置0-1之间,所以说我读0.6,我想在2秒内将音量降低到0,我如何获得百分比以保持音量降低?


这是我的代码..


 private IEnumerator VolumeDown ()

{

    float volumeIncrease = globalVarsScript.musicVolume;

    float volumePercentage = ??;

    float newLerpEndTime = Time.realtimeSinceStartup + 2f;


    while (Time.realtimeSinceStartup < newLerpEndTime)

    {

        audio.volume = volumeIncrease;

        volumeIncrease -= volumePercentage;

        yield return null;

    }

}

抱歉,我无法获取“ volumePercentage”


噜噜哒
浏览 286回答 1
1回答

梦里花落0921

我正在使用Unity并停止时间,Time.timeScale = 0f;因此不能 Time.deltaTime在while循环中使用'Time.realtimeSinceStartup'。您无需为此使用Time.realtimeSinceStartup。的确,设置Time.timeScale为0会使 每帧Time.deltaTime返回0。这就是为什么Time.unscaledDeltaTime在Unity 4.5中添加此地址来解决该问题的原因。只需更换Time.deltaTime用Time.unscaledDeltaTime。您可以使用事件使用if (Time.timeScale == 0)来自动决定使用Time.unscaledDeltaTime还是Time.deltaTime。IEnumerator changeValueOverTime(float fromVal, float toVal, float duration){&nbsp; &nbsp; float counter = 0f;&nbsp; &nbsp; while (counter < duration)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (Time.timeScale == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter += Time.unscaledDeltaTime;&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter += Time.deltaTime;&nbsp; &nbsp; &nbsp; &nbsp; float val = Mathf.Lerp(fromVal, toVal, counter / duration);&nbsp; &nbsp; &nbsp; &nbsp; Debug.Log("Val: " + val);&nbsp; &nbsp; &nbsp; &nbsp; yield return null;&nbsp; &nbsp; }}用法:StartCoroutine(changeValueOverTime(5, 1, 3));值在几秒钟内从5变为。设置为还是都没有关系。13Time.timeScale10
打开App,查看更多内容
随时随地看视频慕课网APP