【问题标题】:How can I get the camera to pause on 3 objects on the x axis?如何让相机在 x 轴上的 3 个对象上暂停?
【发布时间】:2016-11-19 09:21:09
【问题描述】:

我是一名正在从事课堂项目的新学生。在我唯一的场景中,我有 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);
    }

}

【问题讨论】:

  • 您应该将代码的功能放在协程中,而不是简单的延迟。如果明天还没有回答这个问题,请仔细查看并从我的计算机而不是在我的手机上编写一些代码。另外我不建议将它放在您的更新中,因为它不需要。
  • 另请注意,您可以向相机添加动画并播放该动画。
  • 谢谢你....这将是一个很大的帮助。

标签: c# unity3d mono


【解决方案1】:

尝试将此代码放在您的相机上,然后将您希望相机移动到的所有游戏对象放在“对象”列表中。如果您希望相机稍微靠后一点以便它可以看到对象,请创建一个新的 Vector3 而不是简单地给出确切的位置,然后给这个新的 Vector3 迭代对象的 x、y 和 z,然后添加您希望相机与物体保持距离的轴的距离。

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

【讨论】:

  • 所有对象都是带有图像的 3D 平面。它们都沿 x 轴。我希望相机沿 x 轴移动并在每个对象上暂停。我已经为每架飞机分配了一个标签。假设标签是 Image1、Image2、Image3。那么如何将对象添加到列表中以及在哪里。相机现在正在注视 Image1。
  • 好吧……说我笨,但我真的不知道怎么给相机添加一个vector3。相机现在正在查看 Image1,并且处于查看图像的最佳距离。我希望这不会改变。看起来无论图像在场景中的哪个位置,您的代码都可以找到它们……这太酷了……我太兴奋了……迫不及待地想让它工作。我只需要知道如何将图像添加到列表中,并在需要时添加 vector3。
  • 我已经解释了怎么做?而不是 transform.position = Vector3.MoveTowards (transform.position, Objects[iteratingObject].transform.position, Time.deltaTime * speed);写下 Vector3.MoveTowards (transform.position, new Vector3 (Objects[iteratingObject].transform.position.x, Objects[iteratingObject].transform.position.y, Objects[iteratingObject].transform.position.z - distanceFromObject),Time .deltaTime * 速度);其中 distanceFromObject 将是一个公共变量,表示您希望与对象相距多远。
  • 请注意,我是用手机写的这些 cmets,可能需要调整。要放置坐标,只需在相机检查器中的“对象”列表中添加您希望相机移动到的游戏对象。
  • 检查器中没有显示任何内容...甚至没有 MyWait 或速度,我收到一条错误消息:“找不到类型或命名空间名称 List1。你是缺少 using 指令或程序集引用?”指向行 public List Objects; List 下有一条红线 这是那些 tweeks 之一吗?
【解决方案2】:

我认为您需要在 Update 函数中添加一些代码才能使其顺利运行。 Time.deltaTime 仅在 Update 函数中才真正有意义,在此处使用它并尝试在 Start 函数中执行所有操作都行不通。此外,设置 Translate 变换将立即将位置设置为给定值。查找线性插值 (lerp)。

我建议您使用一个成员来跟踪当前状态,即您正在查看的对象,但状态的枚举可能更容易阅读。

然后,您可以在该状态下保留一个成员多长时间,您可以在更新中增加该状态。 然后在更新中,您可以检查是否该更改状态或更新您的移动相机。

祝你好运!

【讨论】:

  • 我以前从未使用过 Unity 或 c#,也从未在 OOL 中编程。所以就像你在跟我说一门外语一样。了解代码应该如何工作以使其正常工作会很有帮助。我非常感谢您的意见,user2287379,因为我知道您真的很想提供帮助。谢谢你。
猜你喜欢
  • 2015-11-25
  • 1970-01-01
  • 2017-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多