Hi! I am still very new to game developing and even less experienced with developing to android so I would appreciate if someone could help me with this. So I've been searching for ways to calibrate the accelerometer when launching the game and I finally found a way that I tought worked but further testing showed that it didn't. The problem along the calibration issue is that the player moves on the Y axis although it shouldn't. Or like it hovers for a little time and then returns to the ground and this happens over and over again when moving the player forward by tilting. As I said I'm still very new to android developing and I really don't understand what I 've copypasted trying to get the calibration right, so this is why I would appreciate it if someone who understands the code better than me took a look. Code (CSharp): public class PlayerMovement : MonoBehaviour { public float moveSpeed; public float maxSpeed = 20; float phoneSpeed = 10; float currentSpeed; public Score point; Matrix4x4 calibrationMatrix; private Rigidbody player; // Use this for initialization public void Start() { point = GetComponent<Score>(); player = GetComponent<Rigidbody>(); #if UNITY_ANDROID calibrateAccelerometer(); #endif } // Update is called once per frame void FixedUpdate() { #if UNITY_STANDALONE || UNITY_WEBPLAYER float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); Vector3 move = new Vector3(moveHorizontal, 0.0f, moveVertical); if (player.velocity.magnitude < maxSpeed) { player.AddForce(move * moveSpeed); } #elif UNITY_ANDROID Vector3 movement = getAccelerometer(Input.acceleration); movement.x = Input.acceleration.x; movement.z = -Input.acceleration.z; if(movement.sqrMagnitude > 1) { movement.Normalize(); } movement *= Time.deltaTime; transform.Translate(movement * phoneSpeed); #endif } void OnTriggerEnter(Collider collider) { if (collider.gameObject.tag == "Coin") { Destroy(collider.gameObject); point.AddScore(); } if (collider.gameObject.tag == "Goal") { GameManager.timeLeft = GameManager.timeLeft + 20; LevelManager.NextLevel(); } } void calibrateAccelerometer() { Vector3 wantedDeadZone = Input.acceleration; Quaternion rotateQuaternion = Quaternion.FromToRotation(new Vector3(0f, 0f, -1f), wantedDeadZone); //create identity matrix ... rotate our matrix to match up with down vec Matrix4x4 matrix = Matrix4x4.TRS(Vector3.zero, rotateQuaternion, new Vector3(1f, 1f, 1f)); //get the inverse of the matrix this.calibrationMatrix = matrix.inverse; } Vector3 getAccelerometer(Vector3 accelerator) { Vector3 accel = this.calibrationMatrix.MultiplyVector(accelerator); return accel; } } Thanks!
Hey I think you solved the problem by now lool but just in case someone else reads this post and wonders what the problem is I think i've spotted the error: In line 56, when you apply the transform to translate, you pass in the "movement" vector which contains the information about all 3 axis. In this case you just want to move along the X and Z axis so you have to pass in each single value in the new vector3 like this: transform.Translate(new Vector3(movement.x, 0f, movement.z) * phoneSpeed); I haven't tried it but it should work