Unity3D Android accelerometer and gyroscope controls

Terence Lam picture Terence Lam · Jul 1, 2014 · Viewed 7.3k times · Source

I'm trying to implement accelerometer/gyroscope controlled game in Unity for Android.

The user will be holding the phone landscape titled at 45 degrees. Depending on his tilt, it will control the pitch of the camera. Depending on his roll, it will control the yaw of the camera.

I've been reading up on both the accelerometer and gyroscope but can't seem to understand out how it can be applied to fit what I need.

Answer

JRowan picture JRowan · Jul 1, 2014

to control your camera from the accelerometer you should use a lowpass filter because the raw accelerometer data will have way to much noise resulting in jittery movement

public float AccelerometerUpdateInterval = 1.0f / 100.0f;
public float LowPassKernelWidthInSeconds = 0.001f;
public Vector3 lowPassValue = Vector3.zero;


Vector3 lowpass(){
        float LowPassFilterFactor = AccelerometerUpdateInterval / LowPassKernelWidthInSeconds; // tweakable
        lowPassValue = Vector3.Lerp(lowPassValue, Input.acceleration, LowPassFilterFactor);
        return lowPassValue;
    }

using the method lowpass() instead of Input.acceleration will make a smooth camera movements when applied to the cameras rotation,