B
B
BadCats2017-10-13 15:49:15
Android
BadCats, 2017-10-13 15:49:15

The camera does not turn with the help of a gyroscope?

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);
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Basmanov, 2017-10-13
@BadCats

You are too smart, Gyroscope.attitude is a quaternion, not a vector, you can't take its components and just use it like that. Have you tried the documentation example, does it work?

using UnityEngine;

public class GyroscopeExample : MonoBehaviour
{
    private void Awake()
    {
        Input.gyro.enabled = true;
    }

    private void Update()
    {
        transform.rotation = Input.gyro.attitude;
    }
}

In general, if you are trying to make a mobile virtual reality, then for this there is a special checkmark in the player settings , turn it on and the camera rotation itself is updated, you don’t even need to do anything.
rRAEI

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question