Skip to content
This repository was archived by the owner on Nov 8, 2019. It is now read-only.

Magic Window

Fred Sauer edited this page Jan 11, 2018 · 10 revisions

To use "Magic Window" mode:

  1. Switch out of VR mode (see SwitchOutOfVr()).
  2. Attach the below controller to the main camera game object, or ancestor thereof, such as a player game object.

Note, if you see laggy gyro behavior updates using this method, see if your device is affected by Unity case 912848.

using UnityEngine;
using UnityEngine.XR;

// Attach this controller to the main camera, or ancestor
// thereof, such as the player game object.
public class GyroController : MonoBehaviour
{
  // Optional, allows user to drag left/right to rotate the world.
  private const float DRAG_RATE = .2f;
  float dragYawDegrees;

  void Start() {
    // Make sure Gyroscope is enabled.
    Input.gyro.enabled = true;
  }

  void Update() {
    if (XRSettings.enabled) {
      // Unity takes care of updating camera transform in VR.
      return;
    }

    // http://android-developers.blogspot.com/2010/09/one-screen-turn-deserves-another.html
    // https://developer.android.com/guide/topics/sensors/sensors_overview.html#sensors-coords
    //
    //     y                                       x
    //     |  Gyro upright phone                   |  Gyro landscape left phone
    //     |                                       |
    //     |______ x                      y  ______|
    //     /                                       \
    //    /                                         \
    //   z                                           z
    //
    //
    //  y
    //  |  z   Unity
    //  | /
    //  |/_____ x
    //

    transform.localRotation =
        // Allow user to drag left/right to adjust direction they're facing.
        Quaternion.Euler(0f, -dragYawDegrees, 0f) *

        // Neutral position is phone held upright, not flat on a table.
        Quaternion.Euler(90f, 0f, 0f) *

        // Sensor reading, assuming default `Input.compensateSensors == true`.
        Input.gyro.attitude *

        // So image is not upside down.
        Quaternion.Euler(0f, 0f, 180f);
  }

  void CheckDrag() {
    if (Input.touchCount != 1) {
      return;
    }

    Touch touch = Input.GetTouch(0);
    if (touch.phase != TouchPhase.Moved) {
      return;
    }

    dragYawDegrees += touch.deltaPosition.x * DRAG_RATE;
  }

Clone this wiki locally