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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在多个航点之间移动摄像机?相关的知识,希望对你有一定的参考价值。

我有这个代码,它将我的相机从一个点移动到另一个点,就像我按下一个按钮,我将从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;
    }
}
答案

你可以使用points数组,如下面的脚本所示。另外为了保持相机的位置,我采用变量currentPointIndex来保持当前点的索引。

CameraMotion.cs

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

基本上,我们循环遍历数组点并相应地进行转换。

或者,如果您的点不是线性形式(在数组中),则可以相应地修改逻辑。但是你可以使用相同的Co-routine。

另一答案

将目标添加到列表中

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

然后在列表中旋转

public void NextView()
{
    currentViewIndex++;
    if(currentViewIndex >= _views.Count) currentViewIndex = 0;


    currentVIEW = _views[currentViewIndex];
}

以上是关于如何在多个航点之间移动摄像机?的主要内容,如果未能解决你的问题,请参考以下文章

Apple Map 中的多个航点

依次播放多个音频片段

ActionScript 3 如何在eclipse中的工作区之间移动片段

无法让我的精灵沿着航点移动

在具有多个摄像头的移动设备上选择默认的备用后置摄像头

如何使用谷歌地图和离子设置航点行程