- Home /
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.
@ahmedbenlakhdhar Did you find the solution to your problem? If you did, would you $$anonymous$$d sharing your code it'd be really helpful. Thanks :)
No sorry, I did not yet find a solution.
Have you tried the Time $$anonymous$$anager? Here you can set the Timestep. This is located under the Edit/Project Settings/Time.
I had a problem with camera vibration in my 2d game when the camera was following a game object. Vibration or Latency depending on the code. The vibration was fix on my project by changing the Fixed Timestep to .0001. I hope this helps. Jake
Answer by vivek.rawat · Dec 06, 2014 at 06:54 PM
hey try using Mathf.SmoothDamp instead of Mathf.Lerp
http://docs.unity3d.com/ScriptReference/Mathf.SmoothDamp.html
Thank you, this function seems to be the right way to make a smooth, but its result is exactly similar to $$anonymous$$athf.Lerp
.
Answer by Ouss · Dec 06, 2014 at 09:13 PM
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;
}
Thank you for replying, but this script is a bit vague.
What are the initial values of xFilt
, yFilt
and zFilt
?
And is kFilterFactor
a constant?
Answer by Jakew01 · Dec 21, 2014 at 01:04 AM
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.
Thank you for your response. Slerp just adds a slow-in and a slows-out to the movement. What I need is a smooth and accurate result at the same time. But I think that after all it has to be balanced.
Your answer
Follow this Question
Related Questions
Smooth Camera/Object Movement 1 Answer
How can I get my camera to reset its position behind the player? 1 Answer
Camera Rendering Skewed Until Transform Adjusted 0 Answers
Crosshair Not Parallel To camera 1 Answer
How Can I Make The Parkour Moves Move Smoothly Instead Of Instantly Teleport? 1 Answer