Hello, I have 2 different script that accomplishes the same thing but both does not work when testing on IOS device. I only need 1 script on the main object which is a 2D roll around ball. I’m using the device accelerometer to move the ball around and I’m using the device start position to match with the accelerometer movement. Everything works fine on unity remote, also when I build on android but for some reason when I build on iOS iPhone the start position doesn’t seem to work I have to play the game with the device completely flat. Perhaps I have to do something with Xcode? Been looking for a solution for days/weeks.
Script 1
float Speed = 15;
float accelY;
void Start()
{
accelY = Input.acceleration.y;
}
void FixedUpdate()
{
float x = Input.acceleration.x;
float y = Input.acceleration.y - accelY;
Vector2 dir = new Vector2(x, y);
if (dir.sqrMagnitude > 1)
dir.Normalize();
dir *= Time.deltaTime;
transform.Translate(dir * Speed, Space.World);
}
Script 2
Matrix4x4 calMatrix;
float Speed = 15;
void Start()
{
Calibrate();
}
void Calibrate()
{
Vector3 accelSnap = Input.acceleration;
Quaternion rotateQua = Quaternion.FromToRotation(new Vector3 (0, 0, -1), accelSnap);
Matrix4x4 matrix = Matrix4x4.TRS(Vector3.zero, rotateQua, new Vector3 (1, 1, 1));
this.calMatrix = matrix.inverse;
}
Vector3 GetAccel(Vector3 accelerator)
{
Vector3 accel = this.calMatrix.MultiplyVector(accelerator);
return accel;
}
void Update()
{
Vector2 dir = GetAccel(Input.acceleration);
if (dir.sqrMagnitude > 1)
dir.Normalize();
dir *= Time.deltaTime;
transform.Translate(dir * Speed);
↧