How to smooth the accelerometer data values

Hello, I am trying to retrieve the acceleration only from the accelerometer and use it to change the camera position smoothly (the gyroscope is not involved).

Here is a piece of code attached to the main camera:

void Update() { 
    float lerpAmount = 0.2f;
    Vector2 cameraTargetPos = (Input.acceleration*-20);
    Vector2 currentPosition = transform.position;
    float xPos = Mathf.Lerp(currentPosition.x, cameraTargetPos.x, lerpAmount);
    float yPos = Mathf.Lerp(currentPosition.y, cameraTargetPos.y, lerpAmount);
    transform.position = new Vector3(xPos, yPos, -10);
}

As can be seen, I am using Mathf.Lerp to make the acceleration data smooth. The problem is that:

  • the smaller lerpAmount is, the more the vibration is noticed;
  • the greater lerpAmount is, the greater the latency is.

I am looking for help to see a better solution than using Mathf.Lerp. Thank you in advance.

hey try using Mathf.SmoothDamp instead of Mathf.Lerp

You need to use a filter lowpass or highpass filter depends on your needs to filter noise from acceleromter data. Here their implementation, you feed a vector3 of the acceleration and get a filtered value:

public Vector3 highPass(Vector3 accVal) {

	Vector3 filteredAcc = lowPass(accVal);

	//resultat = Filtre passe bas - valeur actuelle de l'accéléromètre
	filteredAcc.x -= accVal.x;
	filteredAcc.y -= accVal.y;
	filteredAcc.z -= accVal.z;
	
	return filteredAcc;
	
}

public Vector3 lowPass(Vector3 accVal) {

	xFilt = accVal.x * kFilterFactor + xFilt * (1 - kFilterFactor) ;
	yFilt = accVal.y * kFilterFactor + yFilt * (1 - kFilterFactor) ;
	zFilt = accVal.z * kFilterFactor + zFilt * (1 - kFilterFactor) ;

	Vector3 filteredAcc = new Vector3(xFilt,yFilt,zFilt);
	return filteredAcc;
	
}

Try changing the last line to this:
transform.position = new Vector3.Slerp(xPos, yPos, -10);

http://docs.unity3d.com/ScriptReference/Vector3.Slerp.html

I hope this helps.