Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity3D Android accelerometer and gyroscope controls

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.

like image 293
Terence Lam Avatar asked Jul 01 '14 01:07

Terence Lam


1 Answers

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,

like image 142
JRowan Avatar answered Nov 13 '22 11:11

JRowan