- Home /
Question by
Essexrookie · Apr 05, 2019 at 06:23 PM ·
iosiphoneaccelerometer
how to detect device start position for player acceleration (iPhone)
Hello, I finished a mobile game where I use the phone’s acceleration to move the player. But currently it works correctly only when facing flat as if it was laying on a table. is there a way I can detect device start position on an iPhone so the game can pick up the correct acceleration? This is in landscape mode also in 2D.
Comment
Answer by highpockets · Apr 11, 2019 at 07:29 PM
You will have to calibrate in the start method for example and then multiply the Input by the calibration: (I found this in the forums, posted by @WolfBeardedLion )
private Quaternion calibrationQuaternion;
// Used to calibrate the Input.acceleration
void CalibrateAccelerometer()
{
Vector3 accelerationSnapshot = Input.acceleration;
Quaternion rotateQuaternion = Quaternion.FromToRotation(
new Vector3(0.0f, 0.0f, -1.0f), accelerationSnapshot);
calibrationQuaternion = Quaternion.Inverse(rotateQuaternion);
}
void Start()
{
CalibrateAccelerometer();
}
void Update()
{
Vector3 theAcceleration = Input.acceleration;
Vector3 fixedAcceleration = calibrationQuaternion * theAcceleration;
// Use fixedAcceleration for any logic that follows
}
Your answer