C# 更新位置后,摄像机不断缓慢抖动

C# 更新位置后,摄像机不断缓慢抖动,c#,unity3d,C#,Unity3d,请看这个,告诉我一个解决办法。谢谢 当我的相机到达其最终位置时,布尔标志必须变为false。当前,该标志变为true并保持true。我需要找到一种方法,当我的相机移动到它的第一个目的地时,标志变为假等等 代码如下: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class camMOVE : M

请看这个,告诉我一个解决办法。谢谢

当我的相机到达其最终位置时,布尔标志必须变为false。当前,该标志变为true并保持true。我需要找到一种方法,当我的相机移动到它的第一个目的地时,标志变为假等等

代码如下:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

public class camMOVE : MonoBehaviour {
    public Transform  handleview;
    public Transform pressureview;
    public Transform wallview;
    public Transform sechandleview;
    public Transform pressuretwoview;
    public Transform switchview;

    public GameObject handlebtn;
    public GameObject pressurebtn;
    public GameObject wallbtn;
    public GameObject handletwobtn;
    public GameObject pressuretwobtn;
    public GameObject switchbtn;

    public float transitionSPEED;
    Transform currentVIEW;
    public bool flag = false;
    public bool isReached = false;
    Vector3 currentangel;
    public List<GameObject> modelparts;

    private void Start(){
        handlebtn.SetActive (true);
        pressurebtn.SetActive (false);
        wallbtn.SetActive (false);
        handletwobtn.SetActive (false);
        pressuretwobtn.SetActive (false);
        switchbtn.SetActive (false);

        foreach (GameObject obj in modelparts) {

            obj.GetComponent<BoxCollider> ().enabled = false;
        }
    }

    private void Update(){
        if (flag ) {
            transform.position = Vector3.Lerp (transform.position, 
    currentVIEW.position, Time.deltaTime * transitionSPEED);


            //for camera rotation
            currentangel = new Vector3 (Mathf.LerpAngle 
    (transform.rotation.eulerAngles.x, 
    currentVIEW.transform.rotation.eulerAngles.x, Time.deltaTime * 
    transitionSPEED),
                Mathf.LerpAngle (transform.rotation.eulerAngles.y, 
    currentVIEW.transform.rotation.eulerAngles.y, Time.deltaTime * 
    transitionSPEED),
                Mathf.LerpAngle (transform.rotation.eulerAngles.z, 
    currentVIEW.transform.rotation.eulerAngles.z, Time.deltaTime * 
    transitionSPEED));

            transform.eulerAngles = currentangel;
        }
    }

    public void Handleview(){
        currentVIEW = handleview;
        handlebtn.SetActive (false);
        flag = true;
    }

    public void Pressureview() {
        currentVIEW = pressureview;
        pressurebtn.SetActive (false);
        flag = true;
    }

    public void Wallview() {
        currentVIEW = wallview;
        wallbtn.SetActive (false);
        flag = true;
    }

    public void Secondhandleview(){
        currentVIEW = sechandleview;
        handletwobtn.SetActive (false);
        flag = true;
    }

    public void Pressuretwoview(){
        currentVIEW = pressuretwoview;
        pressuretwobtn.SetActive (false);
        flag = true;
    }

    public void Switchview(){
        currentVIEW = switchview;
        switchbtn.SetActive (false);
        flag = true;

    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类camMOVE:单一行为{
公共交通工具;
公众视野;
公共转换墙视图;
公共改革秘书长;
公共交通压力;
公众视野;
公共游戏对象手柄;
公共游戏对象压力;
公共游戏对象wallbtn;
公共游戏对象handletwotn;
公共游戏对象;
公共游戏对象交换;
公共交通换乘速度;
转换当前视图;
公共布尔标志=假;
公共bool isReached=false;
矢量3电流角;
公开零件清单;
私有void Start(){
把手TN.SetActive(正确);
pressurebtn.SetActive(假);
wallbtn.SetActive(假);
handletwotn.SetActive(假);
压力设置为激活(假);
开关BTN.SetActive(假);
foreach(modelparts中的游戏对象obj){
obj.GetComponent().enabled=false;
}
}
私有void更新(){
国际单项体育联合会(旗){
transform.position=Vector3.Lerp(transform.position,
currentVIEW.position、Time.deltaTime*转换速度);
//用于摄像机旋转
currentangel=新矢量3(Mathf.LerpAngle
(transform.rotation.eulerAngles.x、,
currentVIEW.transform.rotation.eulerAngles.x,Time.deltaTime*
过渡速度),
Mathf.LerpAngle(变换、旋转、欧拉朗格尔、,
currentVIEW.transform.rotation.eulerAngles.y,Time.deltaTime*
过渡速度),
Mathf.LerpAngle(变换、旋转、欧拉格尔斯、z、,
currentVIEW.transform.rotation.eulerAngles.z,Time.deltaTime*
过渡速度);
transform.eulerAngles=currentangel;
}
}
公共无效手册视图(){
currentVIEW=手动查看;
把手TN.SetActive(假);
flag=true;
}
公共空间压力视图(){
当前视图=压力视图;
pressurebtn.SetActive(假);
flag=true;
}
公共墙视图(){
当前视图=墙视图;
wallbtn.SetActive(假);
flag=true;
}
公共无效第二手文件视图(){
currentVIEW=sechandleview;
handletwotn.SetActive(假);
flag=true;
}
公共空间压力图(){
当前视图=压力视图;
压力设置为激活(假);
flag=true;
}
公共视图(){
currentVIEW=开关视图;
开关BTN.SetActive(假);
flag=true;
}
}
  • 你不会在任何地方将其设置为false

  • 你真的不必在更新中这样做,你只需熟悉使用协同程序即可

  • 那就很容易了

    基本形式是

    在您的代码中

    StartCoroutine(MoveCamera());
    
    然后

    private IEnumerator MoveCamera() {
      
      while ( not at the new position ) {
    
        move the object a little
        yield return null;
      }
      set object to final exact position
    }
    
    你会在网上找到1000个例子!作为一项规则,永远不要在Unity中使用Update


    实际上,只要使用Tweeng。。。 一旦你适应了这个。你可以用Tweeng

    特温:

    在你的例子中,你可以简单地说

    StartCoroutine( 3f.Tweeng(
         (p)=>transform.position=p,
         fromPosition,
         toPosition) );
    
    这将在3秒钟内完成(即,
    3f


    享受

    基本上,我同意法蒂的上述回答。 您可以使用如下所示的协同程序

    IEnumerator PerformCameraMovement(Transform transformToMove,Vector3 fromPos,Vector3 toPos,float time)
        {
            float i = 0;
            float rate = 1 / time;
            while (i<1)
            {
                i += Time.deltaTime * rate;
                transformToMove.position = Vector3.Lerp(fromPos,toPos,i);
                yield return 0;
            }
        }
    
    IEnumerator PerformCameraMovement(变换变换移动、矢量3 fromPos、矢量3拓扑、浮点时间)
    {
    浮点数i=0;
    浮动率=1/次;
    
    虽然(你需要的是TWEENG——一行代码:而不是在更新、协同程序或TWEENG之类的东西中,你的问题是一个完美的例子,说明什么时候开始使用Unities和StateMachine我在谷歌上搜索了TWEENG,但找不到任何关于它的东西……你的意思是??Hider——链接就在那里。(绝对不是特温,那是一场噩梦。)答案中有链接,@derHugo!:)这引出了另一个问题,这个问题中的链接又引出了另一个问题…所以这基本上是你想出的?嘿,DH-我真的不记得是谁发起的。你够大了,可以上过答案。unity网站吗(今天已基本崩溃).无论如何,你需要对tweeng进行某种扩展,通常不值得每次编写一个协同程序。
    协同程序就像一个线程。
    。嗯,有点。。但实际上,它除了一个线程之外,什么都是。.所有的协同程序都在Unity的主线程中运行,因此需要一段时间的代码可能会阻止UI线程/使你的应用程序运行即使你在IEnumeraor内也要吊死