0

我使用 Unity 和适用于 Android 的 Google VR SDK 开发了一款 VR 游戏。我希望游戏也可以在没有 VR 耳机的情况下玩。我应该如何实现从 VR 到普通模式的切换,反之亦然?我想在正常模式下使用手机陀螺仪保持 360 度旋转。我在网上浏览了许多脚本,但我找不到任何可以使这成为可能的东西。

我发现可以使用 XRSettings.enabled = true/false(取决于模式)来完成切换模式,但是如何在 Normal(非 VR 模式)下保持 360 度旋转

这是我写的脚本:

公共类 GyroToggleManager : MonoBehaviour {

private int flag = 0;
private Quaternion offset;

IEnumerator SwitchToVR() {
    string desiredDevice = "cardboard";
    XRSettings.LoadDeviceByName(desiredDevice);
    yield return null;
    XRSettings.enabled = true;
    transform.localRotation = Quaternion.identity;
}

IEnumerator SwitchTo2D() {
    Input.gyro.enabled = true;

    // couldn't figure out how to find this.
    offset = ;

    XRSettings.LoadDeviceByName("");
    yield return null;
    transform.localRotation = Quaternion.identity;
}

// Use this for initialization
void Start () {
    if(XRSettings.enabled == false){
        Input.gyro.enabled = true;
    }
}

// Update is called once per frame
void Update () {
    if (XRSettings.enabled) {
        return;
    }

    //Also tried different combinations here nothing worked.
    transform.localRotation = Input.gyro.attitude ;
}

public void StartVR(){
    if(XRSettings.enabled == false){
        StartCoroutine (SwitchToVR ());
    }
}

public void StartN(){
    if(XRSettings.enabled == true){
        StartCoroutine(SwitchTo2D());
    }
}

}

更新脚本:

公共类 GyroToggleManager : MonoBehaviour {

Quaternion offset;

IEnumerator SwitchToVR() {
    string desiredDevice = "cardboard";
    XRSettings.LoadDeviceByName(desiredDevice);
    yield return null;
    XRSettings.enabled = true;
    transform.rotation = Quaternion.identity;
}

IEnumerator SwitchTo2D()
{
    Input.gyro.enabled = true;

    //Get offset.. Subtract Camera rotation from Gyro rotation
    offset = transform.rotation * Quaternion.Inverse(GyroToUnity(Input.gyro.attitude));

    XRSettings.LoadDeviceByName("");
    yield return null;
    XRSettings.enabled = false;
}

private static Quaternion GyroToUnity(Quaternion q)
{
    return new Quaternion(q.x, q.y, -q.z, -q.w);
}

// Use this for initialization
void Start () {
        if(XRSettings.enabled == false){
    Input.gyro.enabled = true;
     }
}

void Update()
{
    if (XRSettings.enabled)
    {
        return;
    }

    //Add the gyro value with the offset then apply to the camera 
    transform.rotation = offset * GyroToUnity(Input.gyro.attitude);
}


public void StartVR(){
    if(XRSettings.enabled == false){
        StartCoroutine (SwitchToVR ());
    }
}

public void StartN(){
    if(XRSettings.enabled == true){
        StartCoroutine(SwitchTo2D());
    }
}

}

4

1 回答 1

1

下面是一个简单的相机跟随脚本,它跟随球员的球,同时保持相机和球员之间的偏移距离。它使用一个偏移值来做到这一点,方法是从玩家的位置中减去相机的位置,然后将该偏移量重新应用到相机的位置以及UpdateorLateUpdate函数中的当前玩家位置。

public Transform playerTransform;
public Transform mainCameraTransform = null;
private Vector3 cameraOffset = Vector3.zero;

void Start()
{

    mainCameraTransform = Camera.main.transform;

    //Get camera-player Transform Offset that will be used to move the camera 
    cameraOffset = mainCameraTransform.position - playerTransform.position;
}

void LateUpdate()
{
    //Move the camera to the position of the playerTransform with the offset that was saved in the beginning
    mainCameraTransform.position = playerTransform.position + cameraOffset;
}

上面的示例和代码并不完全是您的解决方案,但它是了解您需要做什么的最简单方法。

在您的情况下,您需要从陀螺仪传感器或 Input.gyro.attitude. 细微的变化是你不能真正使用-+因为两者都Quaternion不像Vector3上面的例子。

  • 要像我在 函数中所做的那样从另一个中减去a ,乘以另一个的倒数 。逆是用 获得的。QuaternionQuaternionStartVector3QuaternionQuaternion.Inverse

  • 要像我在上面的函数中那样添加两个,只需将两者相乘即可。QuaternionsLateUpdateVector3Quaternion

以下是您的代码中的相关更改:

Quaternion offset;

IEnumerator SwitchTo2D()
{
    Input.gyro.enabled = true;

    //Get offset.. Subtract Camera rotation from Gyro rotation
    offset = transform.rotation * Quaternion.Inverse(GyroToUnity(Input.gyro.attitude));

    XRSettings.LoadDeviceByName("");
    yield return null;
}

// Update is called once per frame
void Update()
{
    if (XRSettings.enabled)
    {
        return;
    }

    //Add the gyro value with the offset then apply to the camera 
    transform.rotation = offset * GyroToUnity(Input.gyro.attitude);
}

private static Quaternion GyroToUnity(Quaternion q)
{
    return new Quaternion(q.x, q.y, -q.z, -q.w);
}

GyroToUnity函数用于将陀螺仪坐标转换为 Unity 的坐标,然后再将其应用于相机。陀螺仪传感器使用右手坐标,而 Unity 的相机和其他对象使用左手坐标。有关更多信息,请参阅

于 2018-04-30T11:12:59.217 回答