我正在同时执行两个 lerp,一个用于使相机居中,另一个用于缩放,使用以下代码:
IEnumerator centerCameraAndZoom(Vector2 source, Vector2 target, float duration, float zoomAmount) {
float t = 0;
while (t < 1) {
// add the time
t += Time.deltaTime / duration;
// smooth it out (easing)
var lerped = Mathf.SmoothStep(0.0f, 1.0f, t);
// zoom in
Camera.main.orthographicSize = Mathf.Lerp(Camera.main.orthographicSize, zoomAmount, lerped);
// move canvas center in the direction of the card
canvas.GetComponent<RectTransform>().anchoredPosition = Vector2.Lerp(source, target, lerped);
yield return null;
}
}
我的问题是,尽管理论上两者的速度相同(两者的 t 相同),但 Canvas 中心的运动需要更长的时间,导致缩放完成后移动延迟。
关于如何处理这个问题,同时仍将代码保留在单个循环内,有什么想法吗?
最佳答案
因为每次执行此循环时,相机的正交尺寸都会用作“开始”:
Mathf.Lerp(Camera.main.orthographicSize, ZoomAmount, lerped);
让我们做一个小实验,五个步骤,0, 0.25, 0.5, 0.75, 1
,我们将跟踪相机尺寸和 Canvas 尺寸的值。
第 0 步(也就是我们开始的地方):
Camera.orthographic size: 10 Zoom Amount: 2 Canvas source size: (10,10) Canvas target size: (2,2) lerped is 0, so no change is made.
步骤 0.25:
lerped = 0.25 Camera.orthographicSize (currently 10!) Mathf.Lerp(10, 2, 0.25) => 8 Camera.orthographicSize => 8 Vector2.Lerp((10,10), (2,2), 0.25) => (8,8)
到目前为止一切都很好!
步骤 0.25:
lerped = 0.25 Camera.orthographicSize (currently 8!) Mathf.Lerp(8, 2, 0.5) => 5 Camera.orthographicSize => 5 Vector2.Lerp((10,10), (2,2), 0.5) => (6,6)
哎呀!它们不匹配!
看看发生了什么?由于相机的当前正交尺寸被用作应用于相机正交尺寸的 Lerp 的“起点”,因此它在真正的线性插值之前加速(在最终的线性插值中)在它看起来完成后的瞬间,这是因为它再次减慢了速度,因为它每帧移动“1 像素的 90%”,但该运动是难以察觉的,看起来已经停止了)。
您需要将原始缩放作为参数传递给此方法并使用它,就像您在 Canvas 上使用Vector2源
一样。
关于c# - 在同一个循环中进行两次线性插值,但其中一次需要更长的时间,为什么? (统一3D),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45106030/