I am creating an iPad game and the controls are based on the accelerometer. Using the unity scripting reference I was able to get the following script to work. However, this script is based on the assumption that “we assume that device is held parallell to the ground” " Home button is in the right hand corner."
So my question is: How would I go about changing that? I can’t seem to find any solutions in the reference. Any ideas?
var speed = 1000.0;
var targetSpeed = 18; //how fast the target moves
function Update ()
{
// we assume that device is held parallel to the ground
// and Home button is in the right hand
var dir : Vector3 = Vector3.zero;
// 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.y = Input.acceleration.x;
dir.z = 0;
// 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*targetSpeed;
// Move object
transform.Translate (dir * speed);
if (transform.position.x > 150 transform.position.x < 875 transform.position.y > -625 transform.position.y < -145 )
{ transform.Translate (dir * speed);
}
else
{ transform.Translate (dir * -speed*1.1);
}
}
Based on this thread here is the solution that I came up with.
However, the averages of the accelerometer only works if var horizontalAcc = Vector3(0,0,-1). I tried changing the value to (0,0,0) and it returned the ipad to the default. Can someone explain this to me??
var speed = 100;
var targetSpeed = 20; //how fast the target moves
var dir : Vector3 = Vector3.zero;
var rot0 : Quaternion; // rotation to correct initial angle
var horizontalAcc = Vector3(0, 0, -1); // see note below ... originally(0,0, -1) This allows the averaging???
function Start(){
// averages the accelerometer during 10 frames
var acc0 = Vector3.zero;
for (var i=0; i<10; i++){
acc0 = acc0 + Input.acceleration;
yield; // returns here next frame
}
acc0 = acc0 / 10.0;
// calculates the rotation necessary to fix the tilt
rot0 = Quaternion.FromToRotation(acc0, horizontalAcc);
}
// rotate the accelerometer readings:
// var reading = rot0 * Input.acceleration;
// acc = Vector3.Lerp(acc, reading, Time.deltaTime);
function Update ()
{
// we assume that device is held parallel to the ground
// and Home button is in the right hand
var reading = rot0 * Input.acceleration;
// reading.z = 0;
// 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 = -reading.y;
dir.y = reading.x;
// dir.z = 0;
// clamp acceleration vector to unit sphere
if (dir.sqrMagnitude > 1)
dir.Normalize();
// if (acc.sqrMagnitude > 1)
// acc.Normalize();
// Make it move 10 meters per second instead of 10 meters per frame...
dir *= Time.deltaTime*targetSpeed;
// acc *= Time.deltaTime*targetSpeed;
// Move object
// transform.Translate (dir * speed);
if (gameGUI.notStarted == false){
if ( abletomove == true)
{
renderer.sharedMaterial = material1;
transform.position.x = Mathf.Clamp(transform.position.x, 150, 875);
transform.position.y = Mathf.Clamp(transform.position.y, -625, -145);
transform.Translate (dir * speed);
}