Lerp Quaternion from Accelerometer

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…

float lateral = Input.acceleration.x;
        float vertical = Mathf.Sqrt(
            Input.acceleration.y * Input.acceleration.y +
            Input.acceleration.z * Input.acceleration.z);
        float angle = -(Mathf.Atan2 ( lateral, vertical) * 180.0f) / Mathf.PI;

        camTransform.localRotation = Quaternion.Euler( 0, 0, angle);

but it’s very shaky. Does anyone know how I could stop it from being so shaky?

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 Unity - Scripting API: Mathf.SmoothDamp

1 Like