-1

您好,我有一个代码,当我按下按钮时,它会将我的主摄像机移动到其他游戏对象位置,就像我按下按钮 1 一样,它将向对象 1 的位置移动,与按钮 2 和 3 相同。现在在我的代码中,我有一个名为 flag 的布尔值这在更新功能中是正确的,然后我现在有多个按钮的多个公共功能,当我按下任何按钮时,布尔值变为真并保持为真,这会导致相机抖动,因为布尔值不断更新请告诉我一种方法,当我的相机最终到达它时位置标志布尔值变为假,当我再次按下另一个按钮时,我将再次变为真这是我的代码

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 GameObject parentobj;
Animator anim;

public float transitionSPEED;
Transform currentVIEW;
private bool flag = 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);

    anim = parentobj.GetComponent<Animator> ();
    anim.SetBool ("start", true);

    //currentVIEW = handleview;
    foreach (GameObject obj in modelparts) {

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

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

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

}

4

2 回答 2

0
if (flag == true)
{
    transform.position = Vector3.Lerp (transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);
    if(Mathf.Approximately((transform.position-currentVIEW.position).sqrmagnitude, 0f))
    {
        transform.position = currentVIEW.position;
        transform.rotation = currentVIEW.rotation;
        flag = false;
        return;
    }
....
于 2018-11-27T15:11:42.010 回答
0

好的,所以我看到的一些问题是您错误地使用了 lerp,lerp 是这个比率的线性插值点 a 和 b。如果你不断地改变 pointA 的位置,这个目的就会失败。您应该在移动时存储起点,并在起点和终点之间移动。或 b 使用MoveTowards.

这是 MoveTowards 和使用 Mathf.Approximately 的示例:

 if (flag == true) {
        // This will move you right to the location you want.
        transform.position = Vector3.MoveTowards (transform.position, currentVIEW.position,  Time.deltaTime * transitionSPEED);  

       if(Mathf.Approximately(Vector3.Distance(transform.position, currentVIEW.position), 0f))
       {
           flag = false;
       }
    } // This is the end bracket for the if statement.

不断更改起始 Lerp 起始位置可能会给出不正确的结果和时间。

于 2018-11-27T15:50:26.077 回答