如何让相机在 x 轴上的 3 个物体上暂停?
How can I get the camera to pause on 3 objects on the x axis?
我是一名新生,正在从事 class 项目。在我唯一的场景中,我有 1 个脚本附加到相机。我希望相机在第一个对象上暂停,滚动到第二个对象并暂停然后滚动到第三个对象并暂停然后结束。将此代码放入更新中,相机将永远不会停止。在 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 函数中执行所有操作是行不通的。同时设置 Translate 变换会立即将位置设置为给定值。查找线性插值 (lerp)。
我建议您使用一个成员来跟踪当前状态,即您正在查看的对象,但状态枚举可能更易于阅读。
然后你可以保留一个成员,直到你处于那个状态的时间,你可以在更新中增加它。
然后在更新中,您可以检查是否到了更改状态或更新移动相机的时间。
祝你好运!
尝试将此代码放在您的相机上,并将您希望相机移动到的所有游戏对象放入对象列表中。如果您希望相机稍微靠后一点以便它可以看到对象,请创建一个新的 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));
}
我是一名新生,正在从事 class 项目。在我唯一的场景中,我有 1 个脚本附加到相机。我希望相机在第一个对象上暂停,滚动到第二个对象并暂停然后滚动到第三个对象并暂停然后结束。将此代码放入更新中,相机将永远不会停止。在 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 函数中执行所有操作是行不通的。同时设置 Translate 变换会立即将位置设置为给定值。查找线性插值 (lerp)。
我建议您使用一个成员来跟踪当前状态,即您正在查看的对象,但状态枚举可能更易于阅读。
然后你可以保留一个成员,直到你处于那个状态的时间,你可以在更新中增加它。 然后在更新中,您可以检查是否到了更改状态或更新移动相机的时间。
祝你好运!
尝试将此代码放在您的相机上,并将您希望相机移动到的所有游戏对象放入对象列表中。如果您希望相机稍微靠后一点以便它可以看到对象,请创建一个新的 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));
}