Search Unity

Calibrating the accelerometer input

Discussion in 'Android' started by _Oscar, Feb 2, 2016.

  1. _Oscar

    _Oscar

    Joined:
    Mar 18, 2015
    Posts:
    11
    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):
    1. public class PlayerMovement : MonoBehaviour
    2. {
    3.  
    4.     public float moveSpeed;
    5.     public float maxSpeed = 20;
    6.     float phoneSpeed = 10;
    7.     float currentSpeed;
    8.     public Score point;
    9.  
    10.     Matrix4x4 calibrationMatrix;
    11.  
    12.     private Rigidbody player;
    13.  
    14.     // Use this for initialization
    15.     public void Start()
    16.     {
    17.         point = GetComponent<Score>();
    18.         player = GetComponent<Rigidbody>();
    19.  
    20. #if UNITY_ANDROID
    21.  
    22.         calibrateAccelerometer();
    23. #endif
    24.  
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void FixedUpdate()
    29.     {
    30. #if UNITY_STANDALONE || UNITY_WEBPLAYER
    31.         float moveHorizontal = Input.GetAxis("Horizontal");
    32.         float moveVertical = Input.GetAxis("Vertical");
    33.  
    34.         Vector3 move = new Vector3(moveHorizontal, 0.0f, moveVertical);
    35.  
    36.         if (player.velocity.magnitude < maxSpeed)
    37.         {
    38.             player.AddForce(move * moveSpeed);
    39.         }
    40.  
    41.  
    42. #elif UNITY_ANDROID
    43.  
    44.         Vector3 movement = getAccelerometer(Input.acceleration);
    45.  
    46.         movement.x = Input.acceleration.x;
    47.         movement.z = -Input.acceleration.z;
    48.  
    49.         if(movement.sqrMagnitude > 1)
    50.         {
    51.             movement.Normalize();
    52.         }
    53.  
    54.         movement *= Time.deltaTime;
    55.  
    56.         transform.Translate(movement * phoneSpeed);
    57. #endif
    58.  
    59.     }
    60.  
    61.     void OnTriggerEnter(Collider collider)
    62.     {
    63.         if (collider.gameObject.tag == "Coin")
    64.         {
    65.             Destroy(collider.gameObject);
    66.             point.AddScore();
    67.         }
    68.  
    69.         if (collider.gameObject.tag == "Goal")
    70.         {
    71.             GameManager.timeLeft = GameManager.timeLeft + 20;
    72.             LevelManager.NextLevel();
    73.         }
    74.     }
    75.  
    76.  
    77.     void calibrateAccelerometer()
    78.     {
    79.         Vector3 wantedDeadZone = Input.acceleration;
    80.  
    81.         Quaternion rotateQuaternion = Quaternion.FromToRotation(new Vector3(0f, 0f, -1f), wantedDeadZone);
    82.         //create identity matrix ... rotate our matrix to match up with down vec
    83.         Matrix4x4 matrix = Matrix4x4.TRS(Vector3.zero, rotateQuaternion, new Vector3(1f, 1f, 1f));
    84.         //get the inverse of the matrix
    85.         this.calibrationMatrix = matrix.inverse;
    86.     }
    87.  
    88.     Vector3 getAccelerometer(Vector3 accelerator)
    89.     {
    90.         Vector3 accel = this.calibrationMatrix.MultiplyVector(accelerator);
    91.         return accel;
    92.     }
    93. }
    Thanks!
     
    Last edited: Feb 2, 2016
  2. GiuseppeGela

    GiuseppeGela

    Joined:
    Aug 10, 2015
    Posts:
    18
    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 :)