Hello, I've been looking for solutions everywhere but it seems like my IOS build is failing to run certain code in the script. I'm trying to detect an iOS device start position to match with the gameobject acceleration. These codes work in unity remote but not when testing on an iPhone I don't know why. I don't want the phone to have to be completely laid flat to be able to use the accelerometer correctly. I have 2 scripts that does the job but only need 1 attached on the gameobject.
Script1
float Speed = 13;
float accelY;
public 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);
}
Script2
Matrix4x4 calMatrix;
float Speed = 13;
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);
}
↧