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 can I move an object by moving my phone around using the accelerometer?

Discussion in 'Scripting' started by sangolemango, Apr 25, 2021.

  1. sangolemango

    sangolemango

    Joined:
    Jul 22, 2018
    Posts:
    7
    I want to move an object by moving my phone around. So for example if I moved my phone forwards by 1 meter, the object also moves by one meter. I read that I can achieve this with the accelerometer but that has not worked for me. The object only moves when i tilt my phone, not when I move the phone. How can I achieve this?

    This is my code so far, I read that the accelerometer reading includes gravity too so I tried to get rid of it with the gyroscope.

    Code (CSharp):
    1.     bool gyroEnabled;
    2.     Gyroscope gyro;
    3.  
    4.     void Awake()
    5.     {
    6.         gyroEnabled = EnableGyro();
    7.     }
    8.  
    9.     bool EnableGyro()
    10.     {
    11.         if (SystemInfo.supportsGyroscope)
    12.         {
    13.             gyro = Input.gyro;
    14.             gyro.enabled = true;
    15.  
    16.             transform.parent.rotation = Quaternion.Euler(90, 90, 0);
    17.  
    18.             return true;
    19.         }
    20.  
    21.         return false;
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.         if (gyroEnabled)
    27.         {
    28.             transform.localRotation = gyro.attitude * new Quaternion(0, 0, 1, 0);
    29.  
    30.             Vector3 acceleration = Input.acceleration;
    31.             Vector3 gyroDir = -transform.up;
    32.  
    33.             Debug.Log("Accel: " + acceleration + " Gyro: " + gyroDir);
    34.  
    35.             transform.parent.position += (acceleration - gyroDir) * Time.deltaTime;
    36.         }
    37.     }
     
  2. sangolemango

    sangolemango

    Joined:
    Jul 22, 2018
    Posts:
    7
    Any ideas?
     
  3. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    what is the Debug.Log logging when you move your phone?
    if it just logs 0, 0, 0, try to add ToString like this: acceleration.ToString("F3") in your log (which will force for up to 3 decimals to be logged)

    while some of your code is rather wierd (like you always adding transform.up to the accerleration), it should still be able to register an acceleration when moving and not rotating the device... maybe the acceleration is just too small to be notable

    tho, actually whatever you are doing it should actually be always moving, even if your device doesnt move at all, because of that aformentioned wierdness with the transform up

    also just as a last bit of information:
    the accelerometer of phones is often really really inaccurate (after all you generally dont need an accurate acceleration for what smart phones usually do)

    it should still be able to tell you when the phones moves and in which direction, but I dont think you will be able to ever exactly match the movement of a virtual object to the movement of the phone by just the accelerometer

    you could look into ARFoundation (ARCore/ ARKit), where in addition to accelerometer/ gyroscope and other sensors, the camera feed is used aswell to map the environment for a more accurate localization and thus movement detection