旋转 parent 使 child 朝某个方向看

Rotate parent so child looks in a certain direction

object 等级制度是这样的:

Player
   - OVRCameraRig
       - TrackingSpace
           - CentralEyeAnchor (Camera)

默认情况下,玩家最初面向正确的区域,但由于这在 VR 中有人可能想要校准或 "home" 玩家,所以相机面向场景而不需要在现实生活中面向那个方向.这是使用 Oculus Quest,所以我基本上希望玩家旋转偏移发生变化,以便相机注视场景。

脚本附加到播放器,vrCamera 作为 CentralEyeAnchor:

        Quaternion quat = Quaternion.Inverse(rotationOffset) * vrCamera.transform.rotation;

        Vector3 euler = quat.eulerAngles;
        euler.x = 0;
        euler.z = 0;
        transform.rotation = Quaternion.Euler(euler);

        rotationOffset = transform.rotation;

首先,找到相机和玩家之间的相对旋转relativeRot,使得playerRotation == cameraRotation * relativeRot

左边两边乘以Inverse(cameraRotation),我们知道然后Inverse(cameraRotation) * playerRotation == relativeRot:

Quaternion relativeRot = Quaternion.Inverse(vrCamera.transform.rotation) * transform.rotation;

然后,确定您希望相机具有的旋转角度。也许使用 Quaternion.LookRotation 和你希望相机看起来的方向:

Vector3 desiredCameraLookAtPoint;
Vector3 desiredCameraLookDirection = desiredCameraLookAtPoint - vrCamera.transform.position;
Quaternion desiredCameraRot = Quaternion.LookRotation(desiredCameraLookDirection);

最后,使用相对旋转和所需的相机旋转求解并指定玩家的旋转:

transform.rotation = desiredCameraRot * relativeRot;