简体   繁体   English

Unity:点击 UI 按钮时平滑的相机移动

[英]Unity: Smooth camera movement on UI button click

I'm trying to make the camera move to a certain position after clicking a UI button.单击 UI 按钮后,我试图让相机移动到某个 position。 For the positioning, I use empty game objects' (CameraPositionStart and CameraPositionFinish) coordinates.对于定位,我使用空游戏对象(CameraPositionStart 和 CameraPositionFinish)坐标。 For some reason, the camera just teleports to the position needed instead of smoothly moving to it.由于某种原因,相机只是传送到所需的 position 而不是平滑地移动到它。 Here's my code:这是我的代码:

using UnityEngine; 
using UnityEngine.UI;

public class CameraMover : MonoBehaviour
{
    public float speed;
    public Button ButtonStartNew;
    public GameObject CameraPositionStart;
    public GameObject CameraPositionFinish;

    private void Update()
    {
        ButtonStartNew.onClick.AddListener(moveCamera);
    }

    public void moveCamera()
    {
        transform.position = Vector3.Lerp(transform.position, CameraPositionFinish.transform.position, Time.deltaTime * speed);
    }
}

This behavior is likely caused because of two reasons.这种行为可能是由两个原因引起的。

The method Update is executed once every frame. Update方法每帧执行一次。 Here every frame you are adding an additional call that should be made when you click the button on the UI.这里的每一帧你都添加了一个额外的调用,当你点击 UI 上的按钮时应该进行调用。 The result of this, I suspect, is that when the several hundred calls to moveCamera all happen at the same time, and despite moveCamera only moving the transform by Lerp(Time.DeltaTime * Speed) -- the object moves closer (since you're lerping) instantly because you're calling it a bunch.我怀疑,这样做的结果是,当对moveCamera的数百个调用同时发生时,尽管moveCamera仅通过Lerp(Time.DeltaTime * Speed)移动变换 - object 移动得更近(因为你' re lerping) 立即因为你称它为一堆。

private void Update()
{
    ButtonStartNew.onClick.AddListener(moveCamera);
}

Consider only adding one call to moveCamera to the on click event.考虑仅向单击事件添加一次对moveCamera的调用。 We can do this in Start or Awake since they're only called once.我们可以在StartAwake中执行此操作,因为它们只被调用一次。

private void Start()
{
    ButtonStartNew.onClick.AddListener(moveCamera);
}

However this along does not solve the problem.然而,这并不能解决问题。 Because if we do that moveCamera will only be called once and the result would be the camera would move very slightly toward the target and stop.因为如果我们这样做, moveCamera只会被调用一次,结果是相机会向目标轻微移动并停止。

Consider implementing a Coroutine that will move the transform over time(every frame) until it has reached the target.考虑实现一个Coroutine ,它将随时间(每一帧)移动变换,直到它到达目标。

private void moveCamera()
{
    // A coroutine runs every frame until it stops returning values
    StartCoroutine(MoveCamera);
}

private IEnumerator MoveCamera()
{
    // check the distance and see if we still need to move towards the destination ​
    while(Vector3.Distance(transform.position, CameraPositionFinish.transform.position) > 1.0f)
   ​{

       transform.position = Vector3.Lerp(transform.position, CameraPositionFinish.transform.position, Time.deltaTime * speed);
   
       // Return  nothing meaningful and wait until next frame​
       yield return null;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM