# car movement with accelrometer

I m working on a car racing game for android and getting problem with the left and right movement of the car with accelrometer…movement is not smooth…anyone can help me.the code is given below…

``````function Update(){
var dir : Vector3 = Vector3.zero;
dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.x;
if (dir.sqrMagnitude > 1)

dir.Normalize();

// Make it move 10 meters per second instead of 10 meters per frame...

dir *= Time.deltaTime;

// Move object
transform.Translate (dir * 10);

}
``````

nobody here to help me???

you have to smooth out the input data from the accelerometer yourself because it is very fuzzy
usually you do this by lerping from the previous smoothed input data to the current input data
this introduces some floaty behaviour but it will get rid of the shaking

smoothInput = Vector3.Lerp(smoothInput,Input.acceleration,Time.deltaTime*6);

Can you give us some more information about this? In what way is the movement not smooth? Is it picking up too much data from the accelerometer, and any light twitch or bump is effecting you car? Or is there another issue?

If you are finding that the accelerometer is too sensitive, look at this page re: optimizing accelerometer data:

My car is moving in xz plane.with this code call turned to the left side and right side movement do not work.my device is in portrait mode…i just want by tilting my device right the car moves towards right smoothly and similarly for left side…

Note how that code is mapped:

``````dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.x;
``````

You can map any input to any part of the Vector you are using for movement.

Simply map the input from your device to the appropriate part of the Vector3 dir and then use it as you need to.

If you want acceleration y to be your forward direction then:

``````dir.z = -Input.acceleration.y;
``````

… this is assuming you are using z as “forward”.

Look at this page for some ideas on how the accelerometer works:
http://hillcrestlabs.com/makingmotion/2012/05/01/the-4-most-popular-motion-sensing-devices/