Search Unity

Lerp Quaternion from Accelerometer

Discussion in 'Scripting' started by LeeDenbigh, Oct 22, 2015.

  1. LeeDenbigh

    LeeDenbigh

    Joined:
    Jul 7, 2014
    Posts:
    48
    I'm trying to smooth the rotation of my screen, which is controlled by the accelerometer. What is the best way, it's working perfectly using this code (attached to the camera), which a member on here gave me...
    Code (CSharp):
    1. float lateral = Input.acceleration.x;
    2.         float vertical = Mathf.Sqrt(
    3.             Input.acceleration.y * Input.acceleration.y +
    4.             Input.acceleration.z * Input.acceleration.z);
    5.         float angle = -(Mathf.Atan2 ( lateral, vertical) * 180.0f) / Mathf.PI;
    6.  
    7.         camTransform.localRotation = Quaternion.Euler( 0, 0, angle);
    but it's very shaky. Does anyone know how I could stop it from being so shaky?
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    The accelerometer values are very accurate and change with small movements so it's probably best to dampen them.

    For example, instead of using Input.acceleration.x/y/z directly in your code you should have your own variables for them (e.g. m_AccelerationX). In each update you can move the current accelerometer values towards those using something like http://docs.unity3d.com/ScriptReference/Mathf.SmoothDamp.html
     
    LeeDenbigh likes this.