我在场景中有六个不同的空游戏对象,有6个不同的按钮 . 当我点击按钮1时,相机将朝向游戏对象1移动并根据目标游戏对象自行旋转但是当相机移向第3个游戏对象时,我的相机将以不同的随机方式开始移动以及随机旋转的不同这是由float t = 0.0f;在代码中,当我调用public void Wallview()然后我的相机移动到对象3但它开始随机旋转随机旋转请帮助我谢谢
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
公共类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 float transitionSPEED;
Transform currentVIEW;
public bool flag = false;
public 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);
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);
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;
isStarted = false;
}
public void Pressureview(){
currentVIEW = pressureview;
pressurebtn.SetActive (false);
flag = true;
isStarted = false;
}
public void Wallview(){
currentVIEW = wallview;
wallbtn.SetActive (false);
flag = true;
isStarted = false;
}
public void Secondhandleview(){
currentVIEW = sechandleview;
handletwobtn.SetActive (false);
flag = true;
isStarted = false;
}
public void Pressuretwoview(){
currentVIEW = pressuretwoview;
pressuretwobtn.SetActive (false);
flag = true;
isStarted = false;
}
public void Switchview(){
currentVIEW = switchview;
switchbtn.SetActive (false);
flag = true;
isStarted = false;
}
}
2 回答
大部分答案都是重复 . 仍然完全重写它为其他读者的背景 .
您可以获取
points
数组,如下面的脚本所示 . 另外为了保持相机的位置,我采用变量currentPointIndex
只是为了保持当前点的索引 .为了正确旋转,请在协程内使用
Quaternion.RotateTowards();
,如下面的代码所示CameraMotion.cs
基本上,我们循环遍历数组点并相应地进行转换 .
或者,如果您的点不是线性形式(在数组中),则可以相应地修改逻辑 . 但是你可以使用相同的Co-routine .
我建议让事情变得更好,首先不要让事情硬编码让它接受更多的对象 . 第二个坚持用更简单的解决方案 .
将这些类放在您的相机上,会发生什么事情是通过AdjustOrientation在您想要查看的对象中获取的第一类 . 更新将在填充后查看该对象 .
第二类允许您单击包含刚体的场景中的任何对象,并且是Kinematic . 单击对象后,它将设置AdjustOrientations使相机查看该对象,您可以轻松地将标记添加到gameObject并说明hit.collider.tag ==“LookAt”然后允许它查看 .