C# 如何在多个航路点之间移动摄像机?

C# 如何在多个航路点之间移动摄像机?,c#,unity3d,C#,Unity3d,我有一个代码,它将我的相机从一个点移动到另一个点,就像我按下一个按钮,我将从a点移动到B点一样。那么如何修改这个代码,使我的相机在多个点之间转换?例如,从A移动到B,然后从B移动到C,依此类推。谢谢 这是我的密码: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class camMOVEtwo : MonoBehaviour

我有一个代码,它将我的相机从一个点移动到另一个点,就像我按下一个按钮,我将从a点移动到B点一样。那么如何修改这个代码,使我的相机在多个点之间转换?例如,从A移动到B,然后从B移动到C,依此类推。谢谢

这是我的密码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class camMOVEtwo : 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;
    private bool isStarted = 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);

        foreach (GameObject obj in modelparts) {

            obj.GetComponent<BoxCollider> ().enabled = false;
        }
    }

    private void Update () {

        if (flag && !isStarted) {

            StartCoroutine (newnew ());
            isStarted = true;
        }
    }

    IEnumerator newnew () {
        float t = 0.0f;
        while (t < 2.0f) {
            t += Time.deltaTime;
            transform.position = Vector3.Lerp (transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);

            //for camera rotation
            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;
            Debug.Log ("coroutine is running");

            yield return null;
        }
    }

    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;
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
第二类:单一行为{
公共交通工具;
公众视野;
公共墙视图;
公共改革秘书长;
公共交通压力;
公众视野;
公共游戏对象手柄;
公共游戏对象压力;
公共游戏对象wallbtn;
公共游戏对象handletwotn;
公共游戏对象;
公共游戏对象交换;
公共游戏对象parentobj;
动画师;
公共交通换乘速度;
转换当前视图;
私有布尔标志=假;
private bool isStarted=false;
矢量3电流角;
公开零件清单;
私有void开始(){
把手TN.SetActive(正确);
pressurebtn.SetActive(假);
wallbtn.SetActive(假);
handletwotn.SetActive(假);
压力设置为激活(假);
开关BTN.SetActive(假);
anim=parentobj.GetComponent();
anim.SetBool(“开始”,true);
foreach(modelparts中的游戏对象obj){
obj.GetComponent().enabled=false;
}
}
私有无效更新(){
如果(标志&&!已启动){
start例程(newnew());
IsStart=true;
}
}
IEnumerator newnew(){
浮球t=0.0f;
而(t<2.0f){
t+=时间增量时间;
transform.position=Vector3.Lerp(transform.position、currentVIEW.position、Time.deltaTime*transitionSPEED);
//用于摄像机旋转
currentangel=新矢量3(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;
Log(“协同程序正在运行”);
收益返回空;
}
}
公共无效手册视图(){
currentVIEW=handleview;
把手TN.SetActive(假);
flag=true;
}
公共空间压力视图(){
当前视图=压力视图;
pressurebtn.SetActive(假);
flag=true;
}
公共虚空墙视图(){
当前视图=墙视图;
wallbtn.SetActive(假);
flag=true;
}
公共无效Secondhandleview(){
currentVIEW=sechandleview;
handletwotn.SetActive(假);
flag=true;
}
公共空间压力图(){
当前视图=压力视图;
压力设置为激活(假);
flag=true;
}
公共视图(){
currentVIEW=开关视图;
开关BTN.SetActive(假);
flag=true;
}
}

将目标添加到列表中

public Transform  handleview;
public Transform pressureview;
public Transform wallview;
public Transform sechandleview;
public Transform pressuretwoview;
public Transform switchview;

// I made this serialized since actually you wouldn't need the 
// single Transforms above anymore but directly reference them here instead
[SerializeField] private List<Transform> _views;

private int currentViewIndex;

private void Awake()
{
    _views = new List<Transform>()
    {
        handleview,
        pressureview,
        wallview,
        sechandleview,
        pressuretwoview,
        switchview
    }

    currentVIEW = handleView;
}

您可以获取一组
,如下面的脚本所示。 为了保持摄像机的位置,我使用变量
currentPointIndex
来保持当前点的索引

CameraMotion.cs

公共转换[]点;
公共int currentPointIndex=0;
公共变换lookAtTarget;
私有void更新()
{
if(Input.GetKeyDown(KeyCode.Space))
{
Start例程(CameraTransition(点[currentPointIndex],1.0f));
currentPointIndex++;
}
}
IEnumerator摄影机转换(转换下一个点,浮动时间)
{
浮点数i=0;
浮动率=1/次;
向量3 fromPos=变换位置;

虽然(顺便说一句,我知道有什么方法比查看
eulerAngles
?嘿@derHugo我设法沿着不同的变换位置移动我的相机,现在我的主要问题是,当我调用公共void Wallview()时我的相机向墙视图的位置移动,但我的相机开始以不同的随机方式沿着不同的随机旋转移动。我想这是由浮动t=0.0f引起的。你能帮我弄清楚吗?谢谢,这不是这个问题的范围。如果你有新问题,请在确保之前没有提出新问题后打开新问题。如果我们有多个目标,例如首先将相机移向目标A,然后移向目标B,您可以更改矢量3[]点以转换[]个目标点,并在需要时将这些点传递给协程。如果需要,我可以为您编辑上述脚本。
public void NextView()
{
    currentViewIndex++;
    if(currentViewIndex >= _views.Count) currentViewIndex = 0;


    currentVIEW = _views[currentViewIndex];
}
public Transform[] points;
public int currentPointIndex=0;
public Transform lookAtTarget;

private void Update()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        StartCoroutine(CameraTransition(points[currentPointIndex],1.0f));
        currentPointIndex++;
    }
}

IEnumerator CameraTransition(Transform nextPoint,float time)
{
    float i = 0;
    float rate = 1 / time;

    Vector3 fromPos = transform.position;

    while (i<1)
    {
        i += Time.deltaTime * rate;
        transform.position = Vector3.Lerp(fromPos,nextPoint.position,i);
        transform.LookAt(lookAtTarget);
        yield return 0;
    }
}}