使用Unity 5.1和Oculus SDK 0.6以及新的内置"Virtual Reality Supported"复选框后,我似乎无法在VR中更改相机的位置。当然,我可以更改相机的"Tranform Position"组件数字(即x、y和z),但当我在播放模式下运行时,显然Oculus的"视觉相机"没有移动。如何移动/更改Oculus的视角,以调整通过2个镜头看到的Unity世界?
GameObject
,将其移动,不要修改VR相机。在Krzysztof的回答基础上,我通常会使用类似于这样的代码(我将此组件附加到VR相机上,将一个空游戏对象作为其父级,然后将该游戏对象设置为vrCameraHolder):
[RequireComponent(typeof(Camera))]
public class VRPlayer : MonoBehaviour
{
// set to be the parent of this object
public Transform vrCameraHolder;
// simple utility functions
public static Quaternion LocalToWorldRotation(Transform t, Quaternion localRot)
{
return t.rotation * localRot;
}
public static Quaternion WorldToLocalRotation(Transform t, Quaternion rot)
{
return Quaternion.Inverse(t.rotation) * rot;
}
// Gets the body part position in world space
public Vector3 GetBodyPartPosition(UnityEngine.XR.XRNode bodyPart)
{
return vrCameraHolder.localToWorldMatrix.MultiplyPoint(UnityEngine.XR.InputTracking.GetLocalPosition(bodyPart));
}
// Gets the body part rotation in world space
public Quaternion GetBodyPartRotation(UnityEngine.XR.XRNode bodyPart)
{
return LocalToWorldRotation(vrCameraHolder, UnityEngine.XR.InputTracking.GetLocalRotation(bodyPart));
}
// moves entire player body so that this becomes true
public void SetBodyPartPosition(UnityEngine.XR.XRNode bodyPart, Vector3 pos)
{
Vector3 bodyPartPos = vrCameraHolder.localToWorldMatrix.MultiplyPoint(UnityEngine.XR.InputTracking.GetLocalPosition(bodyPart));
vrCameraHolder.position += pos - bodyPartPos;
}
// if you want to change rotation you should probably only use this to keep the headset tilted the same way (y axis is up, so 0 is forward along x axis, 180 is backwards along x axis, 90 and 270 are along z axis, etc.)
public void SetBodyPartYRotation(UnityEngine.XR.XRNode bodyPart, float yRot)
{
Vector3 curRot = GetBodyPartRotation(bodyPart).eulerAngles;
SetBodyPartRotation(bodyPart, new Vector3(curRot.x, yRot, curRot.z));
}
// rotates entire player body so that this becomes true (but doesn't change position)
public void SetBodyPartRotation(UnityEngine.XR.XRNode bodyPart, Quaternion rot)
{
Quaternion curWorldRot = GetBodyPartRotation(bodyPart);
Quaternion cancelOutWorldRot = Quaternion.Inverse(curWorldRot);
float angle1;
Vector3 axis1;
cancelOutWorldRot.ToAngleAxis(out angle1, out axis1);
Vector3 bodyPartPosition = GetBodyPartPosition(bodyPart);
vrCameraHolder.RotateAround(bodyPartPosition, axis1, angle1);
// now player rotation = not rotated
float angle2;
Vector3 axis2;
rot.ToAngleAxis(out angle2, out axis2);
vrCameraHolder.RotateAround(bodyPartPosition, axis2, angle2);
}
// rotates entire player body so that this becomes true (but doesn't change position)
public void SetBodyPartRotation(UnityEngine.XR.XRNode bodyPart, Vector3 eulerAngles)
{
SetBodyPartRotation(bodyPart, Quaternion.Euler(eulerAngles));
}
}