Hi,
I am using the accelerometer in my mobile game. I want the player to hold the phone straight and not flat (like on a table) and calibrate the accelerometer on scene start like this:
private void Start()
{
Calibrate();
}
public void Calibrate()
{
Vector3 accelerationSnapshot = Input.acceleration;
Quaternion rotateQuaternion = Quaternion.FromToRotation(
new Vector3(0.0f, 0.0f, -1.0f), accelerationSnapshot);
calibrationQuaternion = Quaternion.Inverse(rotateQuaternion);
}
void Update()
{
Vector3 theAcceleration = Input.acceleration;
Vector3 fixedAcceleration = calibrationQuaternion * theAcceleration;
movement = new Vector3(fixedAcceleration.x*movementSpeed, fixedAcceleration.y*movementSpeed, 0);
}
private void FixedUpdate()
{
rb2D.velocity = movement;
}
This works perfect on Android but building the iOS version, my gameObject just falls down and I have to tilt the phone flat.
Any idea why this is not working?
Specs:
Unity 2018.4.0f1
iOS 12.3 (iPhone 6s)
Xcode 10.2.1
Accelerometer frequency is 60hz
target minimum iOS is 9.0 in Unity