- Home /
Incremental acceleration with Imput
Hey, I'm supernew in Unity... I'm trying to figure how to make a cube move like an ice cube on a tray using the iphone accelerometer. So the trick is that if I increase the angle of inclination the cube has to accelerate incrementally and decelerates if I tilted the device to the opposite side. So far I'm playing with this code from the reference without any luck:
var speed = 10.0;
function Update () { var dir : Vector3 = Vector3.zero;
dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.x;
if (dir.sqrMagnitude > 1)
dir.Normalize();
dir *= Time.deltaTime;
transform.Translate (dir * speed);
}
Can someone help me figure it out?
Thanks!
Answer by fafase · Mar 29, 2012 at 06:28 AM
have a look at acceleration of object on an inclined plane. You would have to get the inclinaison, I am not familiar with iphone but I guess the engine can return the angle easily.
Then your accelration is
g*sin(ang) where g is gravity = 9.8
Since it is a block of ice you could ignore friction. If you want to apply it you would to define it and add to the equation.