在Unity 5中更改相机(Oculus Rift)位置

时间:2015-08-18 12:41:07

标签: unity3d oculus virtual-reality

使用Unity 5.1Oculus SDK 0.6以及新的内置Virtual Reality Supported复选框,我似乎无法更改相机在VR中的位置。

当然我可以更改相机的Tranform Position组件编号(即x和y和z)但是当我在播放模式下运行时,显然Oculus' 看到相机没有移动。

如何移动/更换Oculus'透视,通过2个镜头来调整Unity世界的看法?

2 个答案:

答案 0 :(得分:4)

将父GameObject添加到相机并移动它,不要修改VR相机。

答案 1 :(得分:0)

扩展Krzysztof的答案,我通常使用这样的东西(我将这个组件附加到VR Camera,给它一个空的游戏对象作为父对象,然后将该游戏对象设置为vrCameraHolder):< / p>

[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));
    }
}