根据摄像机阵列平滑移动摄像机位置
Smooth move camera position according to array of cameras
我目前正在尝试使用预定义的摄像头位置数组来平滑地更改摄像头的位置。
它应该是这样的:
- 我按下space,摄像头应该平滑地切换到摄像头阵列中摄像头0的位置。
- 我再次按下space,摄像头应该会平滑地切换到摄像头阵列中摄像头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;
}
}
使用我目前的解决方案,相机实际上平滑地移动到目标。
但是,一旦达到该目标,之后按下 space,相机就会更改为目标,而不会进行平滑处理。
就好像一旦 lerp 成功完成一次,它就不会再 lerp,除非我当然重新启动。
编辑:澄清一下:按下space时,相机目标实际上发生了变化,并且相机跟随这个目标。问题是,一旦相机位置到达目标位置一次,它就不会再平滑地 lerp 到目标,而是立即改变它的位置到目标。
on pressing space afterwards, the camera just changes to the target,
without smoothing.
当您按下 space
时,源和目标都会更改,因此您可以立即设置变换的位置。
once the lerp has succesfully been completed once, it won't lerp ever
again, unless I restart ofcourse.
来自文档:
1. Time.time
This is the time in seconds since the start of the game
2。 Vector3.Lerp
The parameter t (time) is clamped to the range [0, 1]
因此,Time.time
将继续上升,但 Lerp
将限制在最大值 1。当您计算 Time.time * 0.1 (your speed)
时,需要 10 秒才能达到您的目标。超过 10 秒的任何内容都将被限制为 1,这将导致您立即跳转到目的地。请改用 Time.deltaTime
。
此外,您不需要多个摄像头来充当位置目标。一组变换就可以了。
综上所述,它应该是这样的:
public class CameraWaypoints : MonoBehaviour
{
public Transform[] CamPOVs;
public float _speed = 0.1f;
private Camera _main;
private int _indexTargetCamera;
void Awake ()
{
_main = Camera.main;
_indexTargetCamera = 0;
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.Space))
{
_indexTargetCamera = ++_indexTargetCamera % CamPOVs.Length;
}
var time = Time.deltaTime * _speed;
// Alternatives to Lerp are MoveTowards and Slerp (for rotation)
var position = Vector3.Lerp(_main.transform.position, CamPOVs[_indexTargetCamera].position, time);
var rotation = Quaternion.Lerp(_main.transform.rotation, CamPOVs[_indexTargetCamera].rotation, time);
_main.transform.position = position;
_main.transform.rotation = rotation;
}
}
我目前正在尝试使用预定义的摄像头位置数组来平滑地更改摄像头的位置。 它应该是这样的:
- 我按下space,摄像头应该平滑地切换到摄像头阵列中摄像头0的位置。
- 我再次按下space,摄像头应该会平滑地切换到摄像头阵列中摄像头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; } }
使用我目前的解决方案,相机实际上平滑地移动到目标。 但是,一旦达到该目标,之后按下 space,相机就会更改为目标,而不会进行平滑处理。
就好像一旦 lerp 成功完成一次,它就不会再 lerp,除非我当然重新启动。
编辑:澄清一下:按下space时,相机目标实际上发生了变化,并且相机跟随这个目标。问题是,一旦相机位置到达目标位置一次,它就不会再平滑地 lerp 到目标,而是立即改变它的位置到目标。
on pressing space afterwards, the camera just changes to the target, without smoothing.
当您按下 space
时,源和目标都会更改,因此您可以立即设置变换的位置。
once the lerp has succesfully been completed once, it won't lerp ever again, unless I restart ofcourse.
来自文档:
1. Time.time
This is the time in seconds since the start of the game
2。 Vector3.Lerp
The parameter t (time) is clamped to the range [0, 1]
因此,Time.time
将继续上升,但 Lerp
将限制在最大值 1。当您计算 Time.time * 0.1 (your speed)
时,需要 10 秒才能达到您的目标。超过 10 秒的任何内容都将被限制为 1,这将导致您立即跳转到目的地。请改用 Time.deltaTime
。
此外,您不需要多个摄像头来充当位置目标。一组变换就可以了。
综上所述,它应该是这样的:
public class CameraWaypoints : MonoBehaviour
{
public Transform[] CamPOVs;
public float _speed = 0.1f;
private Camera _main;
private int _indexTargetCamera;
void Awake ()
{
_main = Camera.main;
_indexTargetCamera = 0;
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.Space))
{
_indexTargetCamera = ++_indexTargetCamera % CamPOVs.Length;
}
var time = Time.deltaTime * _speed;
// Alternatives to Lerp are MoveTowards and Slerp (for rotation)
var position = Vector3.Lerp(_main.transform.position, CamPOVs[_indexTargetCamera].position, time);
var rotation = Quaternion.Lerp(_main.transform.rotation, CamPOVs[_indexTargetCamera].rotation, time);
_main.transform.position = position;
_main.transform.rotation = rotation;
}
}