【问题标题】:Vector3.Lerp not correctly working?Vector3.Lerp 无法正常工作?
【发布时间】:2015-08-18 19:48:05
【问题描述】:

我正在使用 Unity3d,但遇到 Vector3.Lerp 问题。在我的初始代码中,Lerp 代码工作正常,但 PrevView 方法中的第二个 Lerp 工作不正常。相机只是摇晃并返回到之前的位置。我在哪里犯错了?

代码在这里:

using UnityEngine;
using System.Collections;

public class MoveCamera : MonoBehaviour
{
   Vector3 camPos;
   Vector3 startPos; // for storing Camera's first position
   Transform camTr;
   float speed = 5f;**strong text**

void Start()
{
    camTr = Camera.main.transform;
    camPos = camTr.position;
    startPos = camTr.position;
}

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit) && hit.collider.tag == "Buildings")
        {
            var buildings = GameObject.FindGameObjectsWithTag("Buildings");
            foreach (GameObject go in buildings)
            {
                if (go == hit.collider.gameObject)
                {
                    camPos.x = go.transform.position.x;
                    //camPos.y = go.transform.position.y + 30;
                    camPos.z = go.transform.position.z - 20;
                }
                else
                {
                    go.SetActive(false);
                }
            }
        }
    }
    camTr.position = Vector3.Lerp(camTr.position, camPos, Time.deltaTime * speed);
}

public void PrevView()
{
    camTr.position = Vector2.MoveTowards(camTr.position, startPos, Time.deltaTime * speed);
}

}

【问题讨论】:

    标签: unity3d lerp


    【解决方案1】:

    -首先,它不是“第二个 Lerp”,而是 MoveTowards 函数。

    -Lerp 不应该采用Time.deltaTime * speed 它应该是沿路径的标准化浮点数。

    -换句话说,如果您现在要传递变量,只需使用MoveTowards

    -你的 MoveTowards 函数应该是 Vector3 ,我确定这就是你的意思,对吧?

    -PrevView() 应该是IEnumerator,这样写:

    float threshold = 0.01f;
    IEnumerator PrevView()
    {
        while((camTr.position - startPos).sqrMagnitude > threshold)
        {
            camTr.position = Vector2.MoveTowards(camTr.position, startPos, Time.deltaTime * speed);
            yield return null;
        }
    }
    

    然后使用StartCoroutine("PrevView"); 调用IEnumerator

    【讨论】:

    • 谢谢,安德鲁。我做了你写的一切。但是当我按下调用 StartCoroutine("PrevView") 的 Button 时,Unity 冻结并且没有响应
    【解决方案2】:

    尝试像这样更改 PrevView 功能:

    IEnumerator PrevView()
    {
        float threshold = 0;
        float increment = Time.deltaTime * speed;
        while(threshold < 1)
        {
            camTr.position = Vector3.Lerp(camTr.position, startPos, threshold);
            threshold  += increment;
            yield return new WaitForSeconds(0.02f);
        }
    }
    

    然后用 StartCoroutine 调用它。我还没有测试,所以如果你遇到问题,请告诉我。

    【讨论】:

    • 感谢 Reasurria,但它是一回事。相机移动一点,然后返回到之前的位置
    • 我已经更新了。现在应该没问题,只要 startPos 是您希望相机在一切结束时所在的位置。
    【解决方案3】:

    我解决了这个问题。非常感谢您尝试帮助我

    我的代码在这里:

    using UnityEngine;
    using System.Collections;
    
    public class MoveCamera : MonoBehaviour
    {
        Ray ray;
        RaycastHit hit;
        Vector3 camPos;
        Vector3 startPos; // for storing Camera's first position
        Transform camTr;
        float speed = 2.5f;
        bool b;  // avoiding
    
    void Start()
    {
        b = true;
        camTr = Camera.main.transform;
        camPos = camTr.position;
        startPos = camTr.position;
    }
    
    void Update()
    {
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit) && Input.GetMouseButtonDown(0))
        {
            if (hit.collider.tag == "Buildings")
            {
                var buildings = GameObject.FindGameObjectsWithTag("Buildings");
                foreach (GameObject go in buildings)
                {
                    if (go == hit.collider.gameObject)
                    {
                        camPos.x = go.transform.position.x;
                        camPos.y = go.transform.position.y + 30;
                        camPos.z = go.transform.position.z - 20;
                    }
                    else
                    {
                        go.SetActive(false);
                    }
                }
            }
        }
        if (b)
        {
            camTr.position = Vector3.Lerp(camTr.position, camPos, Time.deltaTime * speed);
        }
    }
    
    public void PrevView()
    {
        b = false;
        StartCoroutine("MoveBack");
    }
    
    IEnumerator MoveBack()
    {
        while ((camTr.position - startPos).sqrMagnitude > 0.01)
        {
            camTr.position = Vector3.MoveTowards(camTr.position, startPos, Time.deltaTime * speed * 10);
            yield return new WaitForSeconds(0.001f);
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2021-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-01
      相关资源
      最近更新 更多