慢慢将Maincamera移到另一个位置

问题描述

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

我尝试使用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;
        
    }
    
}

解决方法

尝试使用光滑的湿气。

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

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;
    }
}
,

将lerp与帧一起使用更容易,并且需要在Update函数中使用它。尝试使用Unity docs中的示例:

  import wx

  class MyFrame(wx.Frame):
      def __init__(self):
          super(wx.Frame,self).__init__(parent=None,title='Hello World')
          panel = wx.Panel(self)

  #        wx.SetOptions(button_color=sg.COLOR_SYSTEM_DEFAULT)


          self.text_ctrl = wx.TextCtrl(panel,pos=(5,5))
          self.text_ctrl.SetBackgroundColour((255,255,255))

          font = wx.Font(10,family = wx.FONTFAMILY_MODERN,style = 0,weight = 90,underline = False,faceName ="",encoding = x.FONTENCODING_DEFAULT)
    
          my_btn = wx.Button(panel,label='Press Me',55))
          my_btn.SetFont(font)
          my_btn.SetBackgroundColour((255,255))
          my_btn.SetForegroundColour((255,255))
  #        my_btn.SetDefaultStyle(wx.TextAttr(wx.RED))
  #        my_btn.AppendText("Red text\n")
  #        my_btn.SetDefaultStyle(wx.TextAttr(wx.NullColour,wx.LIGHT_GREY))
  #        my_btn.AppendText("Red on grey text\n")
  #        my_btn.SetDefaultStyle(wx.TextAttr(wx.BLUE))
  #        my_btn.AppendText("Blue on grey text\n")

          self.Show()

  if __name__ == '__main__':
      app = wx.App()
      frame = MyFrame()
      app.MainLoop()

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...