Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Accelerometer offset

Discussion in 'Android' started by shrapnel92, Jun 12, 2014.

  1. shrapnel92

    shrapnel92

    Joined:
    May 17, 2014
    Posts:
    10
    Hi all, I'm having a problem and I've scratching my head for a few days trying to work out how to fix it.

    I want the 'rest' position of my tablet to be at about 45 degrees, and currently it's with the tablet lying flat.

    It is my understanding that the accelerometer does not work on any kind of rotation, instead it reads the effect of gravity, in 3 dimensions.

    So is it possible to put some kind of 45 degree offset on the accelerometer readings? Every one who has tested it so far says they don't like having to hold the tablet flat!

    Someone in one of the other threads suggested this code:

    Code (CSharp):
    1. accelerator.y = (accelerator.y + 0.5) * 2;
    But I *think* this is for iPhone (?) because it doesn't do anything apart from return errors for me.

    Any help/pointing in the right direction would really be apprenticed, I'm coding in C# - also I did search the forum first but all I got were some posts full of dead links to other posts!

    Thanks!
     
    Last edited: Jun 12, 2014
  2. dwd31415

    dwd31415

    Joined:
    Nov 14, 2013
    Posts:
    25
    I am not sure, that your code will work. But I think you should be able to resolve the compiler error with this:
    Code (CSharp):
    1. float correctedAcceleration = (Input.acceleration.y + 0.5f) * 2;
    You have to use correctedAcceleration instead of Input.acceleration.y afterwards.
     
    Last edited: Jun 13, 2014
    saradesu likes this.
  3. shrapnel92

    shrapnel92

    Joined:
    May 17, 2014
    Posts:
    10
    Hey dwd, thanks very much for the reply, the thing is when I use that code the game still behaves in the same way, I still have to hold the tablet flat :(
    Any chance you have any other ideas?

    Thanks again!
     
  4. dwd31415

    dwd31415

    Joined:
    Nov 14, 2013
    Posts:
    25
    Well I think the error might be the math you are using. If 1 is equal to 360 or 2pi and you want that 0 degree will be equal to 45 degrees than you should add 0.25f to your value(360/4=45;1/4=0.25), so this should work:
    Code (CSharp):
    1. float correctedAcceleration=Input.acceleration.y+0.25f;
     
  5. shrapnel92

    shrapnel92

    Joined:
    May 17, 2014
    Posts:
    10
    Hey again dwd, thanks for helping me out again! Now the gameobject is only rotating on one axis though, I'm beginning to wonder if this is even possible :/
    I've attached an image which I hope can illustrate to anyone looking at this thread what it is that I'm trying to achieve!



    Thanks!
     
  6. shrapnel92

    shrapnel92

    Joined:
    May 17, 2014
    Posts:
    10
    Okay I've moved to trying to solve this issue by using the gyroscope instead of the accelerometer, but so far I've not been able to get any functionality out of it at all.

    I thought that putting

    Code (CSharp):
    1.         void Update ()
    2.         {
    3.  
    4.                 transform.rotation = Input.gyro.attitude;
    5.  
    6.         }

    Would allow me access to the gyro, but there doesn't seem to be any way of updating the transform values via gyro.attitude - can you do a live update of the gyro values and then assign them?
    Is it even possible or am I wasting my time?

    Edit: I also tried setting the gyro.updateInterval in the Start function but I got nothing ...
     
  7. dwd31415

    dwd31415

    Joined:
    Nov 14, 2013
    Posts:
    25
    You have to enable the gryo:
    Code (CSharp):
    1. Input.gyro.enabled = true;
     
  8. dwd31415

    dwd31415

    Joined:
    Nov 14, 2013
    Posts:
    25
    This should work for the offset:
    Code (CSharp):
    1. float CorrectGryo(float rotation,float offset)
    2.     {
    3.         if (rotation-offset > 0)
    4.         {
    5.             return Mathf.Max((rotation - offset), 0);
    6.         }
    7.         else
    8.         {
    9.             return Mathf.Min((rotation - offset), 0);
    10.         }
    11.     }
     
  9. HanSoloYolo

    HanSoloYolo

    Joined:
    May 22, 2017
    Posts:
    19
    THIS LINE OF CODE WORKED!!! THANK YOU!!!
    (Input.acceleration.y + 0.5f) * 2

    Placed it in the FixedUpdate() . Mine is as follows:
    Code (CSharp):
    1. // Player movement in mobile devices
    2.             // Building of force vector
    3.             Vector3 movement = new Vector3(Input.acceleration.x, 0.0f, (Input.acceleration.y + 0.5f) * 2);
    4.             // Adding force to rigidbody
    5.             rigid.AddForce(movement * speed * Time.deltaTime);
     
    Last edited: Oct 25, 2017
  10. marquitoserratet

    marquitoserratet

    Joined:
    Apr 25, 2018
    Posts:
    1
    it worked for me too! (Vector3 movement = new Vector3(Input.acceleration.x, 0.0f, (Input.acceleration.y + 0.5f) * 2);
    but i just wanna know how it works. why do we use Input.acc.y +0.5 instead of -input.acc.z??