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

how to use the gyro to move a object along x axis

Discussion in 'Scripting' started by wilson556, Apr 10, 2014.

  1. wilson556

    wilson556

    Joined:
    Mar 20, 2014
    Posts:
    2
    Hello i am wanting to use my phone or other devise to tilt left and right and move a object.
    any help is appreciated
     
  2. importsjc

    importsjc

    Joined:
    Oct 28, 2012
    Posts:
    7
    same here, this is a MUST have feature for any mobile engine, but i cant find any decent documentation on how this works!

    the best i could find was "Gyro Droid" http://forum.unity3d.com/threads/10...h-and-every-sensor-on-Android-devices-RELASED but you shouldn't have to spend $45 just to get gyro access!
     
  3. importsjc

    importsjc

    Joined:
    Oct 28, 2012
    Posts:
    7
    Ok, after doing more research, i found that is it actually better to use the accelerometer, since more devices have it although gyro's are more precise.

    so here is what i did and it works fine

    Code (csharp):
    1. gameObject.transform.position.x += Input.acceleration.x;
    I dont know if this is the best way to do it but it got the job done. Hope this helps!
     
  4. shadow-river

    shadow-river

    Joined:
    May 29, 2013
    Posts:
    63
    This is what I use in javascript, Hope it helps


    Code (csharp):
    1. var speed = 35.0;
    2.  
    3.  
    4. function Update ()
    5.  {
    6.     this.transform.position.x += -Input.acceleration.x * speed * Time.deltaTime;
    7.  }


    if you want to change wich way you tilt to go left or right replace
    Code (csharp):
    1. var speed = 35.0;
    2.  
    3.  
    with

    Code (csharp):
    1. var speed = -35.0;
    2.  
    3.  
     
    Last edited: Apr 12, 2014
  5. importsjc

    importsjc

    Joined:
    Oct 28, 2012
    Posts:
    7
    Thanks for this! i knew i was missing something! :p