title: How to smooth gyroscope input?
body: <p>I'm making a game in Unity where the game's main camera is controlled by the phone orientation. The problem I have is that the gyroscope's data is very noisy and make the camera rotate in a very jittery way. I tried on different phone to make sure the hardware is not the problem(Galaxy s6 and Sony Xperia T2). </p>
  
  <p>I've tried the following but none seems to work:</p>
  
  <p>-Slerp between current rotation and new attitude(too much jitter no matter the what I multiply Time.deltaTime by)</p>
  
  <pre><code>    //called at each update
      transform.rotation = Quaternion.Slerp(transform.rotation, new Quaternion(-Input.gyro.attitude.x, -Input.gyro.attitude.y, 
                  Input.gyro.attitude.z, Input.gyro.attitude.w), 60 * Time.deltaTime);
  </code></pre>
  
  <p>-Average the last gyro samples (either Euleur angles average or <a href="http://wiki.unity3d.com/index.php/Averaging_Quaternions_and_Vectors" rel="nofollow">Quaternion average</a>; both cases still offer too much jitter no matter how many samples I track)</p>
  
  <pre><code>//called at each update
      if (vectQueue.Count &gt;= 5)
          {
              vectQueue.Enqueue(Input.gyro.attitude.eulerAngles);
              vectQueue.Dequeue();
  
  
  
              foreach (Vector3 vect in vectQueue) {
                  avgr = avgr + vect;
  
              }
              avgr = new Vector3(avgr.x/5, avgr.y/5,avgr.z/5);
              transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(avgr),Time.deltaTime*100);
  
          }
          else
          {
              vectQueue.Enqueue(Input.gyro.attitude.eulerAngles);
          }             
  </code></pre>
  
  <p>-Round the gyroscope data(best solution so far to prevent jitter but obviously this isn't smooth at all)</p>
  
  <p>-Apply high/low pass filter (doesn't seem to do anything)</p>
  
  <pre><code>public float alpha = 0.5f;
  //called at each update
  v1 = Input.gyro.attitude.eulerAngles;
      if (v2== null)
          {
              v2 =v1;
              v3 = Input.gyro.attitude.eulerAngles;
          }
          else
          {
              v3 = (1 - alpha) * v3 + (1 - alpha)*(v1 - v2);
              v2 = v1;
              transform.Rotate(v3);
          }
  </code></pre>
  
  <p>-Copy paste code from people like <a href="http://blog.heyworks.com/how-to-write-gyroscope-controller-with-unity3d/" rel="nofollow">this</a>; but the jitter is way too strong in all cases.</p>
  
  <p>So now I'm at a point where I guess I'll try learning kalman filters and implement it in unity but obviously I'd prefer a black box solution to save time.</p>