我使用此代码同时执行两个lerps,一个用于使相机居中,另一个用于缩放:
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都相同),画布中心的运动仍要花费更长的时间,导致缩放完成后运动延迟。
关于如何处理此问题的任何想法,同时仍将代码保留在单个循环内?
因为每次执行此循环,照相机的正射影像大小都将用作“开始”:
Mathf.Lerp(Camera.main.orthographicSize, zoomAmount, lerped);
让我们做一些实验,分五个步骤,0, 0.25, 0.5, 0.75, 1
我们将跟踪相机大小和画布大小的值。
第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%”时,速度又变慢了,但是这种运动是无法察觉的,并且看起来已经停止了)。
您将需要将原始缩放比例作为参数传递给此方法,并使用它,就像Vector2 source
在画布上使用的那样。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句