簡體   English   中英

如何使相機在x軸上的3個對象上暫停?

[英]How can I get the camera to pause on 3 objects on the x axis?

我是一名新學生,正在從事課堂項目。 我唯一的場景中有1個腳本附加到了相機。 我希望相機在第一個對象上暫停,滾動到第二個對象,然后暫停,然后滾動到第三個對象,然后暫停,然后結束。 將此代碼放在UPDATE中,相機永不停止。 在START處,它會猶豫15秒鍾左右,然后轉到最后一個對象,然后函數停止。 注意延遲設置為10秒。 我嘗試將代碼放入函數中,然后從START調用函數……但是效果不好。 我究竟做錯了什么? 幫助我OB1 ...

還有一件事... START是播放聲音的最佳場所嗎?

using UnityEngine;
using System.Collections;

// I want the camera to pause over the 1st object, scroll to the 2nd object and pause 
// then scroll to the 3rd object and pause then end.  Putting this code in the UPDATE
// the camera never stops.  Here in the START, it hesitates around 15 sec and then it 
// goes right to the last object, then the function stops.  Note the delay set for 10 
// seconds.

public class CameraControl : MonoBehaviour
{
    public float speed;      // How fast to move the camera
    public int moves;        // How many moves to make
    public float MyWait;     // How long to pause over object


    // Use this for initialization
    void Start()
    {
        StartCoroutine(MyDelay());
        for (int y = 1; y <= 2; y++)           // go to the next two objects
        { 
            for (int i = 1; i <= moves; i++)   // Move the camera to the next position
            {
                Camera.main.transform.Translate(new Vector3(1.0f, 0.0f, 0.0f) * speed * Time.deltaTime);
                Debug.LogFormat("moves = {0} ", i);
            }
            StartCoroutine(MyDelay());
        }
    }

    IEnumerator MyDelay()
    {
        yield return new WaitForSeconds(10.0f);
    }

}

我認為您需要在Update函數中放入一些代碼才能使其正常運行。 Time.deltaTime僅在Update函數中才有意義,在此處使用它並嘗試執行Start函數中的所有操作均無效。 同時設置“平移”變換將立即將位置設置為給定值。 查找線性插值(lerp)。

我建議您使用一個成員來跟蹤當前狀態,即您正在查看的對象,但是枚舉狀態可能更容易閱讀。

然后,您可以將成員保持該狀態已有多長時間,可以在更新中增加該成員。 然后,在更新中,您可以檢查是否應該更改狀態或更新移動攝像機。

祝好運!

嘗試將此代碼放置在相機上,然后將要相機移動到的所有游戲對象都放置在“對象”列表中。 如果您希望攝像機向后走一點,以便可以看到對象,則創建一個新的Vector3而不是簡單地給出確切位置,然后將該迭代對象的x,y和z賦予該新Vector3,然后添加相機要與對象隔開的軸的距離。

public float MyWait = 5;     // How long to pause over object
public float speed = 5f;      // How fast to move the camera
public List<GameObject> Objects;      //List of each object for the camera to go to


void Start()
{
    StartCoroutine(MoveToObject(0));
}

IEnumerator MoveToObject(int iteratingObject)
{
    //Wait for however many seconds
    yield return new WaitForSeconds(MyWait);
    bool atDestination = false;

    //Move the camera until at destination
    while (!atDestination)
    {
        yield return new WaitForFixedUpdate();

        transform.position = Vector3.MoveTowards(transform.position, Objects[iteratingObject].transform.position, Time.deltaTime * speed);

        if (transform.position == Objects[iteratingObject].transform.position)
            atDestination = true;
    }

    //Continue iterating until moved over all objects in list
    if(iteratingObject != Objects.Count - 1)
        StartCoroutine(MoveToObject(iteratingObject + 1));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM