How to enable gyroscope camera at current device orientation

后端 未结 1 709
北荒
北荒 2020-12-06 23:44

I would like to enable gyro controlled camera onButtonClick event but I want it to start at the camera\'s current position. Currently when the gyro gets enabled it moves th

相关标签:
1条回答
  • 2020-12-07 00:21

    You need to get the offset camera position in the Awake or Start function. In the Update function, apply that offset value to the value from the gyro sensor.

    It looks like you already know offset should be used but you are not doing that the right way. The confusing part is getting the offset which requires subtracting the current camera rotation from the gyro sensor value.

    To subtract a Quaternion multiply the Inverse not just the Quaternion:

    Quaternion = Quaternion *Quaternion.Inverse

    To add a Quaternion multiply the Quaternion:

    Quaternion = Quaternion * Quaternion.
    

    This is what your code should look like:

    Quaternion offset;
    
    void Awake()
    {
        Input.gyro.enabled = true;
    }
    
    void Start()
    {
        //Subtract Quaternion
        offset = transform.rotation * Quaternion.Inverse(GyroToUnity(Input.gyro.attitude));
    }
    
    void Update()
    {
        GyroModifyCamera();
    }
    
    void GyroModifyCamera()
    {
        //Apply offset
        transform.rotation = offset * GyroToUnity(Input.gyro.attitude);
    }
    
    private static Quaternion GyroToUnity(Quaternion q)
    {
        return new Quaternion(q.x, q.y, -q.z, -q.w);
    }
    
    0 讨论(0)
提交回复
热议问题