# tilt control acceleration

Hey guys,

I'm trying to build in a tilt control game for the iPhone. The phone is held vertically and the object is controlled with tilting left and right.

For that I used this script:

``````// Move object using accelerometer
var speed = 125

function Update () {

var dir : Vector3 = Vector3.zero;

// we assume that device is held parallel to the ground
// and Home button is in the right hand

// remap device acceleration axis to game coordinates:
// 1) XY plane of the device is mapped onto XZ plane
// 2) rotated 90 degrees around Y axis

// dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.x; // Z is the axis which controls left and right.

// clamp acceleration vector to unit sphere
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 * speed);
}

``````

And it works really good actually. My only problem with this is that the control is very linear. Doodle Jump for example, accelerates in a different way. if you tilt it very soft, the character moves slow, if you tilt it hard the character moves fast.

But in this script, the speed of the movement does nothing, the acceleration is always the same.

I would really appreciate any tips on that matter.

``````// clamp acceleration vector to unit sphere
if (dir.sqrMagnitude > 1)
dir.Normalize();

``````

Perhaps this is the problem? If dir typically has magnitude greater than one, this ensures you typically get constant acceleration.