1.) Create a new c# script in Unity3D with this content:
using UnityEngine;
using System.Collections;
public class VROneRotateAround : MonoBehaviour {
public Transform target;
public float distance = 5f;
public Vector3 offset = Vector3.zero;
public bool useAngleX = true;
public bool useAngleY = true;
public bool useAngleZ = true;
public bool useRealHorizontalAngle = true;
public bool resetViewOnTouch = true;
private Quaternion initialRotation = Quaternion.identity;
private Quaternion currentRotation;
private static Vector3 gyroAngles; // original angles from gyro
private static Vector3 usedAngles; // converted into unity world coordinates
private int userSleepTimeOut; // original device SleepTimeOut setting
private bool gyroAvail = false;
void Start() {
Input.compensateSensors = true;
transform.position = (target.position + offset) - transform.forward * distance;
}
void FixedUpdate() {
if (gyroAvail == false) {
if (Input.gyro.attitude.eulerAngles != Vector3.zero && Time.frameCount > 30) {
gyroAvail = true;
initialRotation = Input.gyro.attitude;
target.gameObject.SendMessage("StartFlight");
}
return; // early out
}
// reset origin on touch or not yet set origin
if(resetViewOnTouch && (Input.touchCount > 0))
initialRotation = Input.gyro.attitude;
// new rotation
currentRotation = Quaternion.Inverse(initialRotation)*Input.gyro.attitude;
gyroAngles = currentRotation.eulerAngles;
//usedAngles = Quaternion.Inverse (currentRotation).eulerAngles;
usedAngles = gyroAngles;
// reset single angle values
if (useAngleX == false)
usedAngles.x = 0f;
if (useAngleY == false)
usedAngles.y = 0f;
if (useAngleZ == false)
usedAngles.z = 0f;
if (useRealHorizontalAngle)
usedAngles.y *= -1;
transform.localRotation = Quaternion.Euler (new Vector3(-usedAngles.x, usedAngles.y, usedAngles.z));
transform.position = (target.position + offset) - transform.forward * distance;
}
public static Vector3 GetUsedAngles() {
return usedAngles;
}
public static Vector3 GetGyroAngles() {
return gyroAngles;
}
public void ResetView() {
initialRotation = Input.gyro.attitude;
}
void OnEnable() {
// sensor on
Input.gyro.enabled = true;
initialRotation = Quaternion.identity;
gyroAvail = false;
// store device sleep timeout setting
userSleepTimeOut = Screen.sleepTimeout;
// disable sleep timeout when app is running
Screen.sleepTimeout = SleepTimeout.NeverSleep;
}
void OnDisable() {
// restore original sleep timeout
Screen.sleepTimeout = userSleepTimeOut;
//sensor off
Input.gyro.enabled = false;
}
}
Open the demo scene and attach this script to the VROneSDK prefab.
In the property editor select Cube as Target and enter 6 for distance.
Build the app and test it on a device or use UnityRemote to test the behaviour in the editor.