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