- Home /
How to smoothen/average out player movement when using Input.acceleration?
Hey guys, Unity noob making my first 2D mobile game. I'm making a platformer that utilises the accelerometer for left and right movement similar to Doodle Jump.
This is my movement method (called in Update()):
private void Move()
{
var deltaX = Input.acceleration.x * Time.deltaTime * moveSpeed;
var newXPos = transform.position.x + deltaX;
transform.position = new Vector3(newXPos, transform.position.y, transform.position.z);
}
This works quite well for the most part but I'm noticing that the movement is too sensitive especially when holding the phone vertically and rotating it on its z-axis (best way to describe it lol). This makes the player jerk left and right a bit too much. What would be the best way to smoothen out the left/right movement?
Thanks for the reply but unfortunately the movement is still pretty shakey. I think it improved a little bit though :)
Instead of line 5, write-
posVel = Vector3.zero;
Vector3.SmoothDamp(transform.position, new Vector3(newXPos, transform.position.y, transform.position.z), ref posVel, 0.25f);
I am not exactly sure about the last 2 arguments of the SmoothDamp, but it worked for me.
Thanks for another response :) I can't get this to work. The player isn't moving at all now. I also tried:
transform.position = Vector3.SmoothDamp(transform.position, new Vector3(newXPos, transform.position.y, transform.position.z), ref posVel, 0.25f);
Yeah sorry I forgot to write the transform.position =
.
I am not sure about how smoothDamp works but I think that this might be solved by changing some values like posVel or the fourth argument or maybe the fourth argument could be Time.deltaTime.
Actually, the player does move, just incredibly slowly, and when I try increasing moveSpeed it is still just as shakey.
Would using the gyroscope to maintain a constant acceleration work? I believe that the accelerometer increases in acceleration the further you tilt, which would cause large steps of movement. Also, would lerping help to smoothen out the movement?
Your answer
![](https://koobas.hobune.stream/wayback/20220613023509im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer