作为练习,我尝试编写一个简单的 C# 脚本来执行您在上面描述的操作。我希望这可以作为您自己的起点:
using UnityEngine;
public class CamSwitcher : MonoBehaviour {
public CamSpotInfo[] spots;
[Range(0.1f, 3.0f)] public float anim_speed = 0.3f;
protected int idx = 0;
protected float t_accum = 0.0f;
void Update() {
Transform cam = Camera.main.transform;
cam.position = Vector3.Lerp(cam.position, spots[idx].transform.position, t_accum);
cam.LookAt(spots[idx].target); /* TODO: interpolate target positions */
t_accum += Time.deltaTime * anim_speed;
}
void OnGUI() {
if (GUI.Button(new Rect(10, 70, 200, 30), "next spot: " + spots[idx].name)) {
if (++idx >= spots.Length)
idx = 0;
t_accum = 0.0f;
}
}
}
[System.Serializable]
public class CamSpotInfo {
public Transform transform;
public Transform target;
public string name;
}
为了给数组分配新的“相机点”,首先在 Unity 检查器中手动设置“大小”,然后将新的 GameObjects/transforms 从场景层次结构窗口拖动到检查器中数组的各个元素(例如,这些可以只是空的游戏对象,指示每个“相机点”的相机位置)。然后将相机目标分配给数组中的每个元素,您希望主相机查看每个“相机点”(例如轮子)。最后,为将显示在 GUI 按钮上的每个元素输入一个名称。
在本例中,在播放模式中将仅显示一个 GUI 按钮,用于切换到阵列中的下一个“点”。但你可以轻松修改脚本,让相机直接切换到特定元素...
(不确定这对于本网站是否真的是一个好问题,因为它是非常开放的,因为有很多不同的方法可以为此类功能提出可能的解决方案(涉及许多不同的领域,例如相机运动控制器、路径插值、GUI 等))。
编辑:多个 GUI 按钮
这是上面脚本的修改版本,它使用多个 GUI 按钮,每个相机点一个。每个都可以通过“显示 GUI”复选框在检查器中启用/禁用:
using UnityEngine;
public class CamSwitcher : MonoBehaviour {
public CamSpotInfo[] spots;
[Range(0.1f, 3.0f)] public float animSpeed = 0.3f;
protected uint idx = 0;
protected float t = 0.0f;
void Update() {
Transform cam = Camera.main.transform;
Vector3 dir_target = spots[idx].target.position - cam.position;
Quaternion roti = Quaternion.LookRotation(dir_target);
cam.position = Vector3.Lerp(cam.position, spots[idx].transform.position, t);
cam.rotation = Quaternion.Slerp(cam.rotation, roti, t);
t += Time.deltaTime * animSpeed;
}
void OnGUI() {
Rect rect = new Rect(10, 70, 200, 30);
for (uint i=0; i<spots.Length; ++i) {
if (spots[i].showGUI) {
if (GUI.Button(rect, "switch to " + spots[i].name)) {
idx = i;
t = 0.0f;
}
rect.y += rect.height + 5;
}
}
}
}
[System.Serializable]
public class CamSpotInfo {
public Transform transform;
public Transform target;
public string name;
public bool showGUI = true;
}