【发布时间】:2018-04-13 14:16:20
【问题描述】:
我目前正在尝试使用预定义的摄像头位置数组平滑地更改摄像头的位置。 它应该是这样的:
- 我按空格,相机应该会平滑地切换到相机阵列中相机0的位置。
- 我再次按空格,相机应该会顺利切换到相机阵列中相机1的位置
-
等等
public class CameraWaypoints : MonoBehaviour { public Camera[] CamPOVs; private Camera _main; private int _indexCurrentCamera; private int _indexTargetCamera; private float _speed = 0.1f; void Start () { _indexTargetCamera = 0; _main = Camera.main; //disable all camera's for (int i = 0; i < CamPOVs.Length; i++) { CamPOVs[i].enabled = false; } _indexCurrentCamera = 0; } // Update is called once per frame void Update () { if (Input.GetKeyDown(KeyCode.Space)) { if (_indexTargetCamera < CamPOVs.Length) { _indexCurrentCamera = _indexTargetCamera; _indexTargetCamera++; } } //prevent array out of bounds if (_indexTargetCamera >= CamPOVs.Length) { _indexTargetCamera = 0; } _main.transform.position = Vector3.Lerp(CamPOVs[_indexCurrentCamera].transform.position, CamPOVs[_indexTargetCamera].transform.position, Time.time * _speed); _main.transform.rotation = CamPOVs[_indexTargetCamera].transform.rotation; } }
使用我当前的解决方案,相机实际上可以平滑地移动到目标。 但是,一旦达到该目标,然后按空格键,相机只会更改为目标,而不进行平滑处理。
就好像一旦 lerp 成功完成了一次,它就不会再 lerp 了,除非我当然重新开始。
编辑:澄清一下:按下空格键时,相机目标实际上会发生变化,并且相机会跟随这个目标。问题是,一旦相机位置到达目标位置一次,它就不再平滑地移动到目标,而是立即改变它的位置到目标。
【问题讨论】:
-
听起来像是“缺少重新初始化”的问题,
Start()只执行一次? -
也许你更改目标太快(因为每次调用 Update() 都会检查
GetKeyDown(...)) -
不是问题,因为所有相机移动都在更新函数中处理。
-
问题发生的时候不改目标。它实际上是在相机到达目标之前没有改变目标时发生的。