Solution 1: Speed
Public var simulateaccelerometer: Boolean = false; var speed= 10.0; function Update () {var dir: vector3 = vector3.zero; If (simulateaccelerometer) {dir. X = input. getaxis ("horizontal"); Dir. y = input. getaxis ("vertical");} else {dir. X = input. acceleration. x; Dir. y = input. acceleration. y; // clamp acceleration vector to unit sphereif (dir. sqrmagntasks> 1) dir. normalize (); // make it move 10 meters per second instead of 10 meters per frame ...} dir * = time. deltatime; // move objecttransform. translate (dir * speed );}
You can also change the speed into a force.
Solution 2: Force
Public var force: Float = 1.0; Public var simulateaccelerometer: Boolean = false; function fixedupdate () {var dir: vector3 = vector3.zero; If (simulateaccelerometer) {// using joystick input instead of iPhone accelerometerdir. X = input. getaxis ("horizontal"); Dir. y = input. getaxis ("vertical ");} else {// 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 axisdir. X = input. acceleration. y; Dir. y = input. acceleration. x; // clamp acceleration vector to unit sphereif (dir. sqrmagntasks> 1) dir. normalize ();} Rigidbody. addforce (dir * force );}
I personally feel that solution 1 is flexible and responsive. Solution 2 has inertia and obvious buffering.