0

我正在使用 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);
}

}

4

3 回答 3

2

-首先,它不是“第二个 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

于 2015-08-18T21:11:47.107 回答
0

尝试像这样更改 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 调用它。我还没有测试,所以如果你遇到问题,请告诉我。

于 2015-08-19T05:53:13.390 回答
0

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

我的代码在这里:

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

}

于 2015-08-19T10:06:17.003 回答