在 Unity3D 中使用 lerp 时,如何让我的相机在不同位置之间暂停?
How can I get my camera to momentarily pause between different positions when using lerp in Unity3D?
我有一组位置,我希望我的相机在这些位置之间 move/lerp。有两个按钮(按钮 A 和按钮 B)可触发相机移动位置。如果用户按下按钮 A,相机将 lerp 到阵列中的前一个位置。如果用户按下按钮 B,相机将跳转到阵列中的下一个位置。然而,在移动到一个新位置之前,我希望相机先移动到一个中间位置,暂停几秒钟,然后移动。这是我目前拥有的伪代码:
void Update()
{
if (buttonPress == a) {
positionToMoveTo = positions[currentPosition--];
}
if (buttonpress == b) {
positionToMoveTo = positions[currentPosition++];
}
}
void LateUpdate()
{
camera.lerp(intermediatePosition);
StartCoroutine(pause());
}
IEnumerator pause()
{
yield return new WaitForSeconds(3f);
camera.lerp(positionToMoveTo);
}
虽然这不起作用,因为我在切换相机位置时出现奇怪的抖动,而且我的中间位置并不总是出现。我认为我的问题与执行顺序有关,但我无法弄清楚。任何帮助都会很棒:)
从 LateUpdate
运行 开始每帧 在所有 Update
调用完成后每帧 启动一个新协程!
您可以通过稍微不同的方法避免这种情况:
private bool isIntermediate;
private bool moveCamera;
private void LateUpdate ()
{
if(!moveCamera) return;
if(isIntermediate)
{
camera.lerp(intermediatePosition);
}
else
{
camera.lerp(positionToMoveTo);
}
}
private IEnumerator MoveCamera()
{
moveCamera = true;
isIntermediate=true;
yield return new WaitForSeconds(3f);
isIntermediate=false;
// Wait until the camera reaches the target
while(camera.transform.position == PositionToMoveTo){
yield return null;
}
// Stop moving
moveCamera = false;
// just to be sure your camera has exact the correct position in the end
camera.transform.position = PositionToMoveTo;
}
或者,您可以在没有 LateUpdate
的情况下在协程中完成所有移动(但老实说,我不确定协程是在 Update
之前还是之后完成)
private IEnumerator MoveCamera()
{
float timer = 3f;
while(timer>0)
{
timer -= Time.deltaTime;
camera.lerp(intermediatePosition);
yield return null;
}
// Wait until the camera reaches the target
while(camera.transform.position == PositionToMoveTo){
camera.lerp(PositionToMoveTo);
yield return null;
}
// just to be sure your camera has exact the correct position in the end
camera.transform.position = PositionToMoveTo;
}
如前所述,第二个会更干净 bjt 我不知道在 LateUpdate
中是否需要 运行
注意:Vector3的==
运算符精度为0.00001
。如果您需要更好或更弱的精度,则必须更改为
if(Vector3.Distance(camera.transform.position, PositionToMoveTo) <= YOUR_DESIRED_THRESHOLD)
现在你所要做的就是每次你想改变相机位置时调用你的协程。
void Update()
{
if (buttonPress == a)
{
// Make sure the Coroutine only is running once
StopCoroutine(MoveCamera);
positionToMoveTo = positions[currentPosition--];
StartCoroutine (MoveCamera);
}
if (buttonpress == b)
{
// Make sure the Coroutine only is running once
StopCoroutine (MoveCamera);
positionToMoveTo = positions[currentPosition++];
StartCoroutine (MoveCamera);
}
}
我有一组位置,我希望我的相机在这些位置之间 move/lerp。有两个按钮(按钮 A 和按钮 B)可触发相机移动位置。如果用户按下按钮 A,相机将 lerp 到阵列中的前一个位置。如果用户按下按钮 B,相机将跳转到阵列中的下一个位置。然而,在移动到一个新位置之前,我希望相机先移动到一个中间位置,暂停几秒钟,然后移动。这是我目前拥有的伪代码:
void Update()
{
if (buttonPress == a) {
positionToMoveTo = positions[currentPosition--];
}
if (buttonpress == b) {
positionToMoveTo = positions[currentPosition++];
}
}
void LateUpdate()
{
camera.lerp(intermediatePosition);
StartCoroutine(pause());
}
IEnumerator pause()
{
yield return new WaitForSeconds(3f);
camera.lerp(positionToMoveTo);
}
虽然这不起作用,因为我在切换相机位置时出现奇怪的抖动,而且我的中间位置并不总是出现。我认为我的问题与执行顺序有关,但我无法弄清楚。任何帮助都会很棒:)
从 LateUpdate
运行 开始每帧 在所有 Update
调用完成后每帧 启动一个新协程!
您可以通过稍微不同的方法避免这种情况:
private bool isIntermediate;
private bool moveCamera;
private void LateUpdate ()
{
if(!moveCamera) return;
if(isIntermediate)
{
camera.lerp(intermediatePosition);
}
else
{
camera.lerp(positionToMoveTo);
}
}
private IEnumerator MoveCamera()
{
moveCamera = true;
isIntermediate=true;
yield return new WaitForSeconds(3f);
isIntermediate=false;
// Wait until the camera reaches the target
while(camera.transform.position == PositionToMoveTo){
yield return null;
}
// Stop moving
moveCamera = false;
// just to be sure your camera has exact the correct position in the end
camera.transform.position = PositionToMoveTo;
}
或者,您可以在没有 LateUpdate
的情况下在协程中完成所有移动(但老实说,我不确定协程是在 Update
之前还是之后完成)
private IEnumerator MoveCamera()
{
float timer = 3f;
while(timer>0)
{
timer -= Time.deltaTime;
camera.lerp(intermediatePosition);
yield return null;
}
// Wait until the camera reaches the target
while(camera.transform.position == PositionToMoveTo){
camera.lerp(PositionToMoveTo);
yield return null;
}
// just to be sure your camera has exact the correct position in the end
camera.transform.position = PositionToMoveTo;
}
如前所述,第二个会更干净 bjt 我不知道在 LateUpdate
注意:Vector3的==
运算符精度为0.00001
。如果您需要更好或更弱的精度,则必须更改为
if(Vector3.Distance(camera.transform.position, PositionToMoveTo) <= YOUR_DESIRED_THRESHOLD)
现在你所要做的就是每次你想改变相机位置时调用你的协程。
void Update()
{
if (buttonPress == a)
{
// Make sure the Coroutine only is running once
StopCoroutine(MoveCamera);
positionToMoveTo = positions[currentPosition--];
StartCoroutine (MoveCamera);
}
if (buttonpress == b)
{
// Make sure the Coroutine only is running once
StopCoroutine (MoveCamera);
positionToMoveTo = positions[currentPosition++];
StartCoroutine (MoveCamera);
}
}