你好,我有一个代码,当我按下按钮时,它会将我的主摄像机移动到其他游戏对象的位置,就像我按下按钮1一样,它将朝与按钮2和3相同的对象1的位置移动。现在在我的代码中,我有一个布尔值命名为标志,在更新功能中为true,那么当我按任意按钮时,我现在对多个按钮具有多个公共功能,布尔值变为true并保持为真,这会导致相机抖动,因为布尔值不断更新,请告诉我一种方式,当我的相机到达它的最终位置时,布尔值将变为false,然后再次按下另一个按钮时,它将变为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 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;
}
}
答案 0 :(得分: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;
}
....
答案 1 :(得分:0)
好的,所以我看到的一些问题是您不正确地使用lerp,lerp是以此比率的线性插值点a和b。如果您不断更改pointA的位置,则无法达到此目的。您既可以在移动时存储起点,也可以在起点和终点之间移动。或b使用MoveTowards
。
这里是MoveTowards并使用Mathf的示例。大约:
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的起始位置可能会导致错误的结果和计时。