Как перемещать камеру между несколькими путевыми точками? - PullRequest
0 голосов
/ 27 ноября 2018

У меня есть этот код, который перемещает мою камеру из одной точки в другую, например, если я нажму кнопку, я перейду из точки А в точку Б. Как изменить этот код, чтобы моя камера переходила в несколько точек??Например, перейдите от 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 голосов
/ 27 ноября 2018

Вы можете взять массив 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;
    }
}}

В основном, мы зацикливаемся на точках массива и соответственно осуществляем переход.

В качестве альтернативы, если ваши точки не в линейной форме (в массиве), вы можете соответствующим образом изменить логику.Но вы можете использовать ту же подпрограмму.

0 голосов
/ 27 ноября 2018

Добавьте ваши цели в список

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];
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...