Unity 中 transform.Rotate 的问题
Issues with transform.Rotate in Unity
我一直致力于在 Unity 中编写一个根据头部运动旋转的图形。我在旋转方面遇到了多个问题。
Autowalk class 的示例,我用它来根据用户面向的位置找到图形需要旋转的角度:
public class AutoWalk : MonoBehaviour {
//VR head
private Transform vrHead;
//angular displacement from normal
public float xAng, yAng, zAng;
//previous values
public float xOrig, yOrig, zOrig;
// Use this for initialization
void Start () {
//Find the VR head
vrHead = Camera.main.transform;
//finding the initial direction
Vector3 orig = vrHead.TransformDirection(Vector3.forward);
xOrig = orig.x;
yOrig = orig.y;
zOrig = orig.z;
}
// Update is called once per frame
void Update () {
//find the forward direction
Vector3 forward = vrHead.TransformDirection(Vector3.forward);
float xForward = forward.x;
float yForward = forward.y;
float zForward = forward.z;
//find the angle between the initial and current direction
xAng = Vector3.Angle(new Vector3(xOrig, 0, 0), new Vector3(xForward, 0, 0));
yAng = Vector3.Angle(new Vector3(0, yOrig, 0), new Vector3(0, yForward, 0));
zAng = Vector3.Angle(new Vector3(0, 0, zOrig), new Vector3(0, 0, zForward));
//set new original angle
xOrig = xAng;
yOrig = yAng;
zOrig = zAng;
}
从那里我转到 ReadingPoints class,其中包含图形(球体)和轴(拉伸的立方体)上的所有点:
public class ReadingPoints : MonoBehaviour {
// Update is called once per frame
void Update () {
float xAngl = GameObject.Find("GvrMain").GetComponent<AutoWalk>().xAng;
float yAngl = GameObject.Find("GvrMain").GetComponent<AutoWalk>().yAng;
float zAngl = GameObject.Find("GvrMain").GetComponent<AutoWalk>().zAng;
if ((xAngl != prevXAngl) || (yAngl!=prevYAngl) || (zAngl!=prevZAngl))
{
//rotate depending on the angle from normal
foreach (GameObject o in allObjects)
{
o.transform.Rotate(new Vector3(xAngl, yAngl, zAngl), Space.World);
prevXAngl = xAngl;
prevYAngl = yAngl;
prevZAngl = zAngl;
}
}
allObjects,顾名思义,包含点和轴。
无论如何,运行 程序的第一个问题是图形似乎被撕裂了。
图是如何 supposed to look (这是 o.transform.Rotate(...) 被注释掉时的样子)
这是它的方式 actually looks :( 此外,当我移动时图形不旋转,我不知道为什么(我认为我可能不正确地使用旋转功能但也许我也没有找到正确的角度?)。非常感谢任何关于问题的想法,谢谢!
首先我认为你应该开始使用四元数 (https://docs.unity3d.com/ScriptReference/Quaternion.html)
第二;当你旋转一个附有相机的对象时,它的视线会发生变化,最终对象(图形)将不在视野范围内。如果您希望尽可能多的完整图表尽可能长时间地留在相机视野中。您应该为图形的四元数提供与应用于您的相机或它所连接的对象相反的旋转。
确保图形的所有元素都是图形中中心游戏对象的子元素,并且只操纵该点。
我一直致力于在 Unity 中编写一个根据头部运动旋转的图形。我在旋转方面遇到了多个问题。
Autowalk class 的示例,我用它来根据用户面向的位置找到图形需要旋转的角度:
public class AutoWalk : MonoBehaviour {
//VR head
private Transform vrHead;
//angular displacement from normal
public float xAng, yAng, zAng;
//previous values
public float xOrig, yOrig, zOrig;
// Use this for initialization
void Start () {
//Find the VR head
vrHead = Camera.main.transform;
//finding the initial direction
Vector3 orig = vrHead.TransformDirection(Vector3.forward);
xOrig = orig.x;
yOrig = orig.y;
zOrig = orig.z;
}
// Update is called once per frame
void Update () {
//find the forward direction
Vector3 forward = vrHead.TransformDirection(Vector3.forward);
float xForward = forward.x;
float yForward = forward.y;
float zForward = forward.z;
//find the angle between the initial and current direction
xAng = Vector3.Angle(new Vector3(xOrig, 0, 0), new Vector3(xForward, 0, 0));
yAng = Vector3.Angle(new Vector3(0, yOrig, 0), new Vector3(0, yForward, 0));
zAng = Vector3.Angle(new Vector3(0, 0, zOrig), new Vector3(0, 0, zForward));
//set new original angle
xOrig = xAng;
yOrig = yAng;
zOrig = zAng;
}
从那里我转到 ReadingPoints class,其中包含图形(球体)和轴(拉伸的立方体)上的所有点:
public class ReadingPoints : MonoBehaviour {
// Update is called once per frame
void Update () {
float xAngl = GameObject.Find("GvrMain").GetComponent<AutoWalk>().xAng;
float yAngl = GameObject.Find("GvrMain").GetComponent<AutoWalk>().yAng;
float zAngl = GameObject.Find("GvrMain").GetComponent<AutoWalk>().zAng;
if ((xAngl != prevXAngl) || (yAngl!=prevYAngl) || (zAngl!=prevZAngl))
{
//rotate depending on the angle from normal
foreach (GameObject o in allObjects)
{
o.transform.Rotate(new Vector3(xAngl, yAngl, zAngl), Space.World);
prevXAngl = xAngl;
prevYAngl = yAngl;
prevZAngl = zAngl;
}
}
allObjects,顾名思义,包含点和轴。
无论如何,运行 程序的第一个问题是图形似乎被撕裂了。 图是如何 supposed to look (这是 o.transform.Rotate(...) 被注释掉时的样子) 这是它的方式 actually looks :( 此外,当我移动时图形不旋转,我不知道为什么(我认为我可能不正确地使用旋转功能但也许我也没有找到正确的角度?)。非常感谢任何关于问题的想法,谢谢!
首先我认为你应该开始使用四元数 (https://docs.unity3d.com/ScriptReference/Quaternion.html) 第二;当你旋转一个附有相机的对象时,它的视线会发生变化,最终对象(图形)将不在视野范围内。如果您希望尽可能多的完整图表尽可能长时间地留在相机视野中。您应该为图形的四元数提供与应用于您的相机或它所连接的对象相反的旋转。
确保图形的所有元素都是图形中中心游戏对象的子元素,并且只操纵该点。