【问题标题】:Moving Maincamera slowly to another position将主摄像机缓慢移动到另一个位置
【发布时间】:2021-01-12 01:43:13
【问题描述】:

我想在按下按钮时将主摄像机从一个点缓慢移动到另一个点。此按钮调用具有移动相机代码的方法。

我曾尝试使用 Lerp 方法,但摄像机位置变化太快,以至于当我单击按钮时摄像机直接移动到新位置。我想让相机慢慢移动到一个新的位置。

下面是我的代码,谁能帮帮我。

================================================ ============================

using System.Collections;
using System.Collections.Generic;

using UnityEngine;

public class Cameramove : MonoBehaviour
{
    public GameObject cam;
    Vector3 moveToPosition;// This is where the camera will move after the start
    float speed = 2f; // this is the speed at which the camera moves
    public void move()
    {
        //Assigning new position to moveTOPosition
        moveToPosition = new Vector3(200f, 400f, -220f);
        float step = speed * Time.deltaTime;
        cam.transform.position = Vector3.Lerp(cam.transform.position, moveToPosition, step);
        cam.transform.position = moveToPosition;
        
    }
    
}

【问题讨论】:

    标签: c# unity3d transform lerp


    【解决方案1】:

    将 lerp 与框架一起使用更容易,您需要在更新函数中使用它。尝试使用Unity docs 中的示例:

    public int interpolationFramesCount = 45; // Number of frames to completely interpolate between the 2 positions
    int elapsedFrames = 0;
    
    void Update()
    {
        float interpolationRatio = (float)elapsedFrames / interpolationFramesCount;
    
        Vector3 interpolatedPosition = Vector3.Lerp(Vector3.up, Vector3.forward, interpolationRatio);
    
        elapsedFrames = (elapsedFrames + 1) % (interpolationFramesCount + 1);  // reset elapsedFrames to zero after it reached (interpolationFramesCount + 1)
    
        Debug.DrawLine(Vector3.zero, Vector3.up, Color.green);
        Debug.DrawLine(Vector3.zero, Vector3.forward, Color.blue);
        Debug.DrawLine(Vector3.zero, interpolatedPosition, Color.yellow);
    }
    

    【讨论】:

      【解决方案2】:

      尝试使用光滑的潮湿。

      这是您应该尝试的新代码:

      using System.Collections;
      using System.Collections.Generic;
      
      using UnityEngine;
      
      public class Cameramove : MonoBehaviour
      {
          Vector3 matric;
          public GameObject cam;
          Vector3 moveToPosition;
          float speed = 2f; 
          bool move_ = false;
      
          void Update(){
               if(move_){
                    //Assigning new position to moveTOPosition
                    moveToPosition = new Vector3(200f, 400f, -220f);
                    cam.transform.position = 
                    Vector3.SmoothDamp(cam.transform.position,
                                  moveToPosition,
                                  ref matric, speed);
               }
          }
      
          public void move()
          {
              move_ = true;
          }
      }
      

      【讨论】:

      • 感谢您的回答,但是当我单击按钮时,相机移动了一点,要到达新位置,我必须多次单击按钮。有没有办法通过单击按钮将相机顺利移动到新位置?
      • 我编辑了答案,这样它就可以工作了。很抱歉第一次没有成功,我没有意识到它需要在Update方法上
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-19
      相关资源
      最近更新 更多