smooth accelerometer

Hi friends!!

I am finalizing details of my first game for Android and I had some doubts.

I am making a game in which the movement is with the accelerometer. The inconvenience is that when I’m on the bus or if the device vibrates (a message arrives), this interferes with the player’s movement. It moves crazy everywhere, and the gaming experience becomes very bad. I need to soften this movement a bit to improve that experience. Any suggestions?

THANKS A LOT!

My script movement.

public class MovimientoPersonaje : MonoBehaviour {

	[SerializeField]
	public float speed = 10f;
	public SpriteRenderer spriteRotar;
	public Rigidbody2D boxDudeRigid;

	public float variablePosicion = 0f;

	MatrixCalibrate calibration;

	public void MovimientoAcelerometro () {

		Vector3 dir = Vector3.zero;
		dir.x = (Mathf.Abs (_InputDir.x) > 0.07f) ? _InputDir.x : 0;

		if(dir.sqrMagnitude > 1)
			dir.Normalize();

		dir*= Time.deltaTime;
		transform.Translate(dir * speed);

		if(_InputDir.x < -0.02){
			spriteRotar.flipX = true;
		} 
		if(_InputDir.x > 0.02){
			spriteRotar.flipX = false;
		}
	}
	//Method for calibration 

	//Method to get the calibrated input 
	Vector3 getAccelerometer(Vector3 accelerator){
		Vector3 accel = this.calibration.calibrationMatrix.MultiplyVector(accelerator);
		return accel;
	}
	//Finally how you get the accelerometer input
	Vector3 _InputDir;

	// Use this for initialization
	void Start () {
		calibration = GameObject.Find ("Calibrate").GetComponent<MatrixCalibrate> ();
	}
	// Update is called once per frame
	void Update () {
		_InputDir = getAccelerometer(Input.acceleration);
		//then in your code you use _InputDir instead of Input.acceleration for example 
		//transform.Translate (_InputDir.x, 0, -_InputDir.z);
		MovimientoAcelerometro ();
	}
}

You could either decrease accelerometer sensitivity or like your saying smooth it. To smooth it some you could use mathf.round and use mathf.lerp with that too and that should help.

Hope this helped some!

Any help???

Try smoothing the acceleration with a lerp. See the NOT tested code below.
Change the value of T and that will change the smoothing sensitivity. Please note that this code is not time independent and will smooth faster on faster devices.

    bool first = true;
    Vector3 previousAccel = Vector3.zero;

    Vector3 GetSmoothedAcceleration()
    {
        if (first)
        {
            previousAccel = Input.acceleration;
            first = false;
        }

        Vector3 smoothedAccel = Vector3.Lerp(Input.acceleration, previousAccel, 0.1f);
        previousAccel = smoothedAccel;
        return smoothedAccel;
    }

You could also do a moving average (rolling average) of the Input.acceleration values to have a mean value of the X previous milliseconds.