# Rotate an object smoothly by using Accelerometer Input in Unity

There is a simple script where the object orientation is equal to the mobile devices orientation.

it works but the object doesn't rotate smoothly.

void FixedUpdate()
{
float accelx = Input.acceleration.x;

float angle = Mathf.Asin(accelx) * Mathf.Rad2Deg;

transform.eulerAngles = new Vector3(angle , 0, 0);
}


How to rotate an object smoothly by using Accelerometer Input ?

• Accelerometers are inherently noisy sensors that can't reliably distinguish between orientation and accelerating motion. Usually we use sensor-fusion to combine their input with other sensors to get a better-calibrated estimate of our orientation. Have you considered using Input.gyro.attitude instead? – DMGregory Mar 7 '19 at 20:55

## 1 Answer

DMGregory is right in his comment. What you're looking for is a gyroscope. An Accelerometer measures the acceleration of a device (or the change in velocity). If the object is still, or moving at a constant speed, it will measure 0 (except in the direction of gravity because gravity is a constant acceleration downward). Since force = mass * acceleration, and our mass is constant, you could look at this as indirectly measuring the force applied to the device. If we shake the device we apply a force. If we move the device left, we apply a force pushing left, then another pushing right to stop it once we've reached our destination.

A Gyroscope is used to measure the rotation / orientation of a device. This is what you want. While neither sensor is perfect, you definitely want the gyroscope.

If you REALLY want to use the accelerometer you could do the following, however I would NOT recommend it as it doesn't have much use and a gyroscope will give better results as that's what it's made for:

1. Ask your user to hold the device flat, and measure the accelerometer. This is the direction of gravity as a vector since as long as the device is stationary that should be the only force applied
2. Now if your user rotates the device and the device has no forces applied to it (usually not moving as it's hard for a user to maintain constant velocity), then again the only force on the accelerometer is gravity. Take that measurement and compare it to your original measurement.

Let's say the user held it perfectly flat and the accelerometer measured <0, -9.8, 0> assuming the y axis is pointed towards the ceiling, z away from the player, and x towards the players left hand side. Then say your next measurement is <0, 0, -9.8>. This means the user pointed the controller up towards the ceiling, as the second vector is a 90deg rotation compared to the first. Again this fails as soon as the user tries to move the device, and likely while the user is rotating the device, since the accelerometer might not be located directly in the center of the device or the user is not rotating around the center. This means the accelerometer is moving in the xyz plane, thus a force was applied, thus your measurement could be invalid.