Using raw values directly from the accelerometer sensor to move or rotate a GameObject can cause problems such as jerky movements or vibrations. It is recommended to smooth out the values before using them. In fact, values from the accelerometer sensor should always be smoothed out before use. This can be accomplished with a low pass filter and this is where Vector3.Lerp
comes into place.
//The lower this value, the less smooth the value is and faster Accel is updated. 30 seems fine for this
const float updateSpeed = 30.0f;
float AccelerometerUpdateInterval = 1.0f / updateSpeed;
float LowPassKernelWidthInSeconds = 1.0f;
float LowPassFilterFactor = 0;
Vector3 lowPassValue = Vector3.zero;
void Start()
{
//Filter Accelerometer
LowPassFilterFactor = AccelerometerUpdateInterval / LowPassKernelWidthInSeconds;
lowPassValue = Input.acceleration;
}
void Update()
{
//Get Raw Accelerometer values (pass in false to get raw Accelerometer values)
Vector3 rawAccelValue = filterAccelValue(false);
Debug.Log("RAW X: " + rawAccelValue.x + " Y: " + rawAccelValue.y + " Z: " + rawAccelValue.z);
//Get smoothed Accelerometer values (pass in true to get Filtered Accelerometer values)
Vector3 filteredAccelValue = filterAccelValue(true);
Debug.Log("FILTERED X: " + filteredAccelValue.x + " Y: " + filteredAccelValue.y + " Z: " + filteredAccelValue.z);
}
//Filter Accelerometer
Vector3 filterAccelValue(bool smooth)
{
if (smooth)
lowPassValue = Vector3.Lerp(lowPassValue, Input.acceleration, LowPassFilterFactor);
else
lowPassValue = Input.acceleration;
return lowPassValue;
}