Search Unity

Reset Accelerometer? Is it possible?

Discussion in 'iOS and tvOS' started by sbuchbinder, Nov 7, 2008.

  1. huxley

    huxley

    Joined:
    Apr 27, 2009
    Posts:
    323
    I just ended up putting the FixAcceleration function in the control script and it was much easier to access.
     
  2. se7en

    se7en

    Joined:
    Dec 3, 2008
    Posts:
    230
    Thanks Fuel Games guys and Little Angel - I need to calibrate the accelerometer and had no idea how to do it. I dropped your script into my Game Engine and my UI HUD controller... literally 5 minutes later, it's perfect. I cannot say thank you enough around here - Unity FTW : )
     
  3. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,671
  4. stockydog

    stockydog

    Joined:
    Dec 24, 2010
    Posts:
    9
    Thanks cheezorg. If you are still around on these forums I appreciate your solution. This works great.
     
  5. rustbucket

    rustbucket

    Joined:
    Jul 14, 2013
    Posts:
    2
    I realize this thread is kind of old but I'm having difficulties getting the "fix" from Fuel Games to work with my existing script (Sorry there's a bunch of logic in there to find platform and requested input type). I suspect its where I'm trying to get the calibrated vaue but I'm not sure. Normally I write in Unity.js so it could also be my C# translation skills aren't up to the task.

    At any rate any help would be appreciated, thanks in advance!

    r.

    Code (csharp):
    1.  
    2.     void calibrateAccelerometer() {
    3.         Vector3 wantedDeadZone = iPhoneInput.acceleration;
    4.         Quaternion rotateQuaternion = Quaternion.FromToRotation(new Vector3(0f, 0f, -1f), wantedDeadZone);
    5.    
    6.         //create identity matrix ... rotate our matrix to match up with down vec
    7.         Matrix4x4 matrix = Matrix4x4.TRS(Vector3.zero, rotateQuaternion, new Vector3(1f, 1f, 1f));
    8.         //get the inverse of the matrix
    9.         this.calibrationMatrix = matrix.inverse;
    10.     }
    11.    
    12.     Vector3 getAccelerometer(Vector3 accelerator) {
    13.         Vector3 accel = this.calibrationMatrix.MultiplyVector(accelerator);
    14.         return accel;
    15.     }
    16.    
    17.    
    18.     void Update ()
    19.     {  
    20.         Vector3 forward = Camera.main.transform.TransformDirection(Vector3.forward);
    21.         forward.y = 0;
    22.         forward = forward.normalized;
    23.        
    24.         Vector3 forwardForce = new Vector3();
    25.        
    26.         if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android)
    27.         {
    28.             if(tiltControls == 0) {
    29.                 //tilt off
    30.                 float tmpSpeed = moveJoy.GetComponent<Joystick>().position.y;
    31.                 forwardForce = forward * tmpSpeed * 1f * moveSpeed;
    32.             } else {
    33.                 //tilt on
    34.                 float tmpSpeed = Input.acceleration.y *8;              
    35.                 forwardForce = forward * tmpSpeed * 1f * moveSpeed;
    36.                 getAccelerometer(forwardForce);
    37.             }  
    38.            
    39.         }
    40.         else
    41.         {
    42.             forwardForce = forward * Input.GetAxis("Vertical") * moveSpeed;
    43.         }
    44.         rigidbody.AddForce(forwardForce);
    45.  
     
  6. Issah

    Issah

    Joined:
    Jun 3, 2014
    Posts:
    2
  7. Basyan

    Basyan

    Joined:
    May 31, 2015
    Posts:
    1
    As in my case execute calibration, week I am puzzled with this question, helps nothing.
    Code (CSharp):
    1.  void Start () {
    2.      mainCamera = Camera.main.transform;
    3.      thisRigidbody = GetComponent<Rigidbody>();
    4. }
    5. void Update () {
    6.  
    7.      float h = (Input.acceleration.x);
    8.      float v = (-Input.acceleration.z);
    9.    
    10.      if (mainCamera != null)
    11.      {
    12.          mainCameraForward = Vector3.Scale (mainCamera.forward, new Vector3 (1, 0, 1));
    13.          move = (v * mainCameraForward + h * mainCamera.right);
    14.      }
    15.      else
    16.      {
    17.          move = (v * Vector3.forward + h * Vector3.right);
    18.      }
    19.      if (useTorque)
    20.      {
    21.          thisRigidbody.AddTorque(new Vector3(move.z, 0, -move.x) * moveSpeed);
    22.      }
    23.      else
    24.      {
    25.          thisRigidbody.AddForce(move * moveSpeed);
    26.      }
    27. }
     
  8. sputnikbar

    sputnikbar

    Joined:
    Jan 3, 2013
    Posts:
    77
    Hi,

    sorry for Necro posting, but I can't adapt this code to my needs :
    Here, I want to change the Physics.gravity with the accelerometer (for a classic Roll-a-ball game). Here's what I've tried :
    Code (CSharp):
    1.     void Start(){
    2.  
    3.         GravityStart = new Vector3 (
    4.             Mathf.Clamp (Input.acceleration.x, -1, 1),
    5.             Mathf.Clamp (Input.acceleration.z, -0.05f, 0.05f),
    6.             Mathf.Clamp (Input.acceleration.y, -1, 1)
    7.         ) * g;
    8.         Quaternion rotateQ = Quaternion.FromToRotation (new Vector3 (0f, 0f, -1f), GravityStart);
    9.         Matrix4x4 matrix = Matrix4x4.TRS (Vector3.zero, rotateQ, new Vector3 (1f, 1f, 1f));
    10.         this.calibrationMatrix = matrix.inverse;
    11.     }
    12.  
    13.     Vector3 getAccelerometer(Vector3 accelerator){
    14.         Vector3 accel = this.calibrationMatrix.MultiplyVector (accelerator);
    15.         return accel;
    16.     }
    17.     void Update() {
    18.         // normalize axis
    19.         Physics.gravity = new Vector3 (
    20.             Mathf.Clamp (Input.acceleration.x, -1, 1),
    21.             Mathf.Clamp (Input.acceleration.z, -0.05f, 0.05f),
    22.             Mathf.Clamp (Input.acceleration.y, -1, 1)
    23.         ) * g;
    24. Physics.gravity=getAccelerometer(Physics.gravity);
    25. }
    Has anyone an idea ?
    all the best,
    L.