Search Unity

  1. We want you to join us at GDC this year! Take a peek at all of the events we will be hosting during the week of GDC.
    Dismiss Notice
  2. Tell us about your experience here and you’ll get early access to the 2018 Game Studios report + more goodies.
    Dismiss Notice
  3. Unity 2017.3 has arrived! Read about it here.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  5. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice

Need Help > Calibration for Android

Discussion in 'Scripting' started by JeffJason, Sep 12, 2017.

  1. JeffJason

    JeffJason

    Joined:
    Sep 12, 2017
    Posts:
    21
    So i am making a game using sensors of phone.ı want to calibrate, so device can calculate the position of the phone on startup.
    This is the code;

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Hareket : MonoBehaviour {
    // Use this for initialization
    public Rigidbody rb;
    public float zhizi;


    void Start () {

    }

    // Update is called once per frame
    void FixedUpdate () {
    transform.Translate (Input.acceleration.x, -Input.acceleration.y, zhizi);
     
  2. JeffJason

    JeffJason

    Joined:
    Sep 12, 2017
    Posts:
    21
    Still looking for it.
     
  3. JeffJason

    JeffJason

    Joined:
    Sep 12, 2017
    Posts:
    21
    Found the solution;

    1. //declare matrix and calibration vector
    2. Matrix4x4 calibrationMatrix;
    3. Vector3 wantedDeadZone = Vector3.zero;

    4. //Method for calibration
    5. void calibrateAccelerometer()
    6. {
    7. wantedDeadZone = Input.acceleration;
    8. Quaternion rotateQuaternion = Quaternion.FromToRotation(new Vector3(0f, 0f, -1f), wantedDeadZone);
    9. //create identity matrix ... rotate our matrix to match up with down vec
    10. Matrix4x4 matrix = Matrix4x4.TRS(Vector3.zero, rotateQuaternion, new Vector3(1f, 1f, 1f));
    11. //get the inverse of the matrix
    12. calibrationMatrix = matrix.inverse;
    13. }
    14. //Method to get the calibrated input
    15. Vector3 getAccelerometer(Vector3 accelerator){
    16. Vector3 accel = this.calibrationMatrix.MultiplyVector(accelerator);
    17. return accel;
    18. }
    19. //Finally how you get the accelerometer input
    20. Vector3 _InputDir;
    21. void Update()
    22. {
    23. _InputDir = getAccelerometer(Input.acceleration);
    24. //then in your code you use _InputDir instead of Input.acceleration for example
    25. transform.Translate (_InputDir.x, 0, -_InputDir.z);
    26. }
    27. //you also want to calibrate the device on start so regardless of how user is holding it the initial position will be treated as 0 Input.
    28. void Start()
    29. {
    30. calibrateAccelerometer();
    31. }
     
  4. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    979