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

时间:2018-11-27 16:58:07

标签: c# unity3d

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

2 个答案:

答案 0 :(得分:0)

将目标添加到列表

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

答案 1 :(得分:0)

您可以采用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;
    }
}}

基本上,我们在数组点上循环并相应地过渡。

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