hi, i did a code to make my gameobject move when my iPhone/iPad is tilted, but i’m not satisfied of my gameobject’s movement and i’m wondering if there is a way to make the movement smoother. Here is my code :
private var speed = 10.0;
function Update ()
{
var dir : Vector3 = Vector3.zero;
dir.x = Input.acceleration.x;
if (dir.sqrMagnitude > 0.3)
{
dir.Normalize();
dir *= Time.deltaTime;
transform.Translate (dir * speed);
}
}
thanks
Check answer by @aldonaletto for details in this question Accelerometer question.
The script from the answer is:
// Move object in XY using accelerometer (home button at right hand)
var speed: float = 6.0;
var filter: float = 5.0;
private var accel: Vector3;
function Start(){
accel = Input.acceleration;
}
function Update(){
// filter the jerky acceleration in the variable accel:
accel = Vector3.Lerp(accel, Input.acceleration, filter * Time.deltaTime);
// map accel -Y and X to game X and Y directions:
var dir = Vector3(-accel.y, accel.x, 0);
// limit dir vector to magnitude 1:
if (dir.sqrMagnitude > 1) dir.Normalize();
// move the object at the velocity defined in speed:
transform.Translate(dir * speed * Time.deltaTime);
}