This repository was archived by the owner on Nov 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Magic Window
Fred Sauer edited this page Jan 11, 2018
·
10 revisions
To use "Magic Window" mode:
- Make sure None is added as one of the Player Settings > XR Settings > Virtual Reality SDKs.
- At runtime, make sure you switch out of VR mode using
VRSettings.LoadDeviceByName(""); yield null;, see SwitchOutOfVr() for an example how to do this, so that Unity no longer updates the camera transform. - When you're not in VR mode, you probably want to lock screen rotation to "Landscape Left".
- See comment in issue 602 for an example of how to change
Screen.orientationandScreen.autorotate*properties at runtime through script. - To set the non-VR default orientation in Player Settings:
- Temporarily disable Player Settings > XR Settings > Virtual Reality Supported so that the Default Orientation properties are editable.
- Set Player Settings > Resolution and Presentation > Default Orientation to Landscape Left.
- Re-enable Player Settings > XR Settings > Virtual Reality Supported.
- See comment in issue 602 for an example of how to change
- Use the below controller script, attached 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;
}