Unity3D:当时间尺度增加时 Mathf Lerp 太快
Unity3D: Mathf Lerp too fast when timescale increases
我有一张图像,其填充量组件由 Mathf Lerp 控制。问题是,当时间尺度增加时,完成 Mathf Lerp 函数的时间比预期减少得更多。
当时间刻度等于 2 时,函数应该花费一半的时间来完成,但实际上花费的时间更少。知道为什么吗?
public static float demolishTime = 6.0f
public void OnClickDemolish()
{
InvokeRepeating("demolishProgress", 0f, 0.1f);
}
void demolishProgress()
{
progress += (Time.deltaTime / demolishTime);
demolishProgressBar[DemolishManager.demolishState].fillAmount = (float)Mathf.Lerp(0, 1, progress);
if (progress >= 1) demolishCompleted();
}
如果我错了,可能有人会纠正我,但这可能是因为 InvokeRepeating
的第 3 个参数 repeatRate 不受时间刻度的影响。
您可以考虑改用协程,如下所示:
public static float demolishTime = 6.0f;
public void OnClickDemolish() {
StartCoroutine(demolishProgress());
}
IEnumerator demolishProgress() {
float progressedTime = 0f;
// Assuming 'demolishTime' is the time taken to entirely demolish the thing.
while (progressedTime < demolishTime) {
yield return new WaitForEndOfFrame();
progressedTime += Time.deltaTime;
demolishProgressBar[DemolishManager.demolishState].fillAmount = Mathf.Lerp(0, 1, progressedTime);
}
demolishCompleted();
}
我有一张图像,其填充量组件由 Mathf Lerp 控制。问题是,当时间尺度增加时,完成 Mathf Lerp 函数的时间比预期减少得更多。
当时间刻度等于 2 时,函数应该花费一半的时间来完成,但实际上花费的时间更少。知道为什么吗?
public static float demolishTime = 6.0f
public void OnClickDemolish()
{
InvokeRepeating("demolishProgress", 0f, 0.1f);
}
void demolishProgress()
{
progress += (Time.deltaTime / demolishTime);
demolishProgressBar[DemolishManager.demolishState].fillAmount = (float)Mathf.Lerp(0, 1, progress);
if (progress >= 1) demolishCompleted();
}
如果我错了,可能有人会纠正我,但这可能是因为 InvokeRepeating
的第 3 个参数 repeatRate 不受时间刻度的影响。
您可以考虑改用协程,如下所示:
public static float demolishTime = 6.0f;
public void OnClickDemolish() {
StartCoroutine(demolishProgress());
}
IEnumerator demolishProgress() {
float progressedTime = 0f;
// Assuming 'demolishTime' is the time taken to entirely demolish the thing.
while (progressedTime < demolishTime) {
yield return new WaitForEndOfFrame();
progressedTime += Time.deltaTime;
demolishProgressBar[DemolishManager.demolishState].fillAmount = Mathf.Lerp(0, 1, progressedTime);
}
demolishCompleted();
}