随时间推移两个值之间的Lerp
我正在尝试将浮点数减少一个时间值,我正在使用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’
谢谢。
我正在使用Unity并停止
Time.timeScale = 0f;
所以不能使用Time.deltaTime
所以在while循环中使用’Time.realtimeSinceStartup’。
您不需要为此使用Time.realtimeSinceStartup
。 将Time.timeScale
设置为0 Time.deltaTime
使Time.deltaTime
每帧返回0 。
这就是在Unity 4.5中添加Time.unscaledDeltaTime
来解决这个问题的原因。 只需将Time.deltaTime
替换为Time.deltaTime
Time.unscaledDeltaTime
。 您可以使用if (Time.timeScale == 0)
自动决定是否使用Time.unscaledDeltaTime
或Time.deltaTime
。
IEnumerator changeValueOverTime(float fromVal, float toVal, float duration) { float counter = 0f; while (counter < duration) { if (Time.timeScale == 0) counter += Time.unscaledDeltaTime; else counter += Time.deltaTime; float val = Mathf.Lerp(fromVal, toVal, counter / duration); Debug.Log("Val: " + val); yield return null; } }
用法 :
StartCoroutine(changeValueOverTime(5, 1, 3));
该值在3
秒内从5
变为1
。 如果Time.timeScale
设置为1
或0
则Time.timeScale
。