public class vr : MonoBehaviour {
public float X
{
set { x = value; }
get {return x; }
}
public float Y
{
set { y = value; }
get { return y; }
}
public float Z
{
set { z = value; }
get { return z; }
}
float x, y, z;
Transform CameraTransform;
// Use this for initialization
void Start () {
CameraTransform= GetComponent<Transform>();
}
// Update is called once per frame
void Update ()
{
X = Input.gyro.rotationRate.x;
Y = Input.gyro.rotationRate.y;
Z =Input.gyro.rotationRate.z;
RotGyro(X,Y ,Z );
//Cos angle=(a1*b1+a2*b2+a3*b3)/sqrt(a1^2+a2^2+a3^2)*sqrt(b1^2+b2^2+b3^2)
}
float cos_angle;
float sum1;
float sum2;
public void RotGyro(float x,float y,float z)
{
if (Input.gyro.enabled)
{
bool EnabledGyro = true;
Debug.Log("GyroEnabled");
if (y >0)
RotLeft(X, Y, Z, cos_angle);
}
else
{
RotRight(X, Y, Z, cos_angle);
}
sum1 = Mathf.Pow(X, 2) + Mathf.Pow(Y, 2) + Mathf.Pow(Z, 2);
sum2 = Mathf.Pow(Input.gyro.attitude.x, 2) + Mathf.Pow(Input.gyro.attitude.y, 2) + Mathf.Pow(Input.gyro.attitude.z, 2);
cos_angle = (Input.gyro.attitude.x * X + Input.gyro.attitude.y * Y + Input.gyro.attitude.z * Z) / (Mathf.Sqrt(sum2) * Mathf.Sqrt(sum1));
}
void RotLeft(float x, float y, float z, float angle)
{
transform.RotateAround(transform.position, transform.position.x - cos_angle * Time.deltaTime);
}
void RotRight(float x, float y, float z, float angle)
{
transform.RotateAround(transform.position, transform.position.x +cos_angle * Time.deltaTime);
}
}