在 Unity 5 中更改相机 (Oculus Rift) 位置
Changing Camera (Oculus Rift) Position in Unity 5
使用 Unity 5.1
和 Oculus SDK 0.6
以及新的内置 Virtual Reality Supported
复选框,我似乎无法在 VR 中更改相机的位置。
当然我可以更改相机的 Tranform Position
组件编号(即 x 和 y 和 z)但是当我 运行 在播放模式下时,很明显 Oculus 的 看到相机没动
如何 move/change 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));
}
}
使用 Unity 5.1
和 Oculus SDK 0.6
以及新的内置 Virtual Reality Supported
复选框,我似乎无法在 VR 中更改相机的位置。
当然我可以更改相机的 Tranform Position
组件编号(即 x 和 y 和 z)但是当我 运行 在播放模式下时,很明显 Oculus 的 看到相机没动
如何 move/change 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));
}
}