【问题标题】:how to update only once in unity如何统一更新一次
【发布时间】:2018-11-27 15:03:52
【问题描述】:

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

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

}

【问题讨论】:

  • 不要使用“if (flag == true)”,而是使用“if (flag)”,这样会更好
  • @Daveloper 还是一样
  • 我觉得你的很多问题都可以通过搜索Unity3D自己的文档来解决。 docs.unity3d.com/ScriptReference

标签: c# unity3d lerp


【解决方案1】:
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;
    }
....

【讨论】:

  • 让我试试这个然后我会告诉你
  • @developer sqrmagnitude 是什么?
  • @developer 这不起作用,它仍在更新,相机仍在晃动
  • sqrmagnitude 的计算成本较低(它是幅度的平方,所以它只是 x X x + y X y + z X z,而不是平方根)如果您的相机抖动,这意味着你的 transform.position 会不断变化。尝试用 0.01f 替换 Mathf.Epsilon。这个想法是让它在某个时候停止。我很困惑它是否在摇晃,因为 Lerp 应该将它渐近到你的最终位置,这不应该“摇晃”:/
  • @develper 是的,它完成了这项工作,但是使用这个 0.01f 相机会立即停止,我的游戏中有一些其他条件无法使用此方法,请告诉任何其他方法
【解决方案2】:

好的,所以我看到的一些问题是您错误地使用了 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 的起始位置可能会产生不正确的结果和时间。

【讨论】:

  • 我颠倒了 MoveTowards 的顺序,编辑了解决这个问题的答案,所以它应该是 Vector3.MoveTowards (transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);
  • 让我试试这个
  • 我收到这些错误错误 CS1061:类型 UnityEngine.Vector3' does not contain a definition for distance' 并且找不到扩展方法 distance' of type UnityEngine.Vector3'。您是否缺少程序集参考?还有这个错误 CS1501:方法 Approximately' takes 1' 参数没有重载
  • @NoumanKhan 我修正了错别字,Vector3.Distance 有一个大写字母 D,我把这一切都写在这里了。关于近似值,我在上面固定了那条线。
  • 现在收到此错误“错误 CS1501:方法 Distance' takes 1' 参数没有重载”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-20
  • 2022-01-26
  • 2017-04-06
  • 1970-01-01
  • 2021-01-30
  • 1970-01-01
  • 2011-01-31
相关资源
最近更新 更多