Search Unity

Using Accelerometer for Object Movement in AR Scene

Discussion in 'AR' started by tqd_hshl, Sep 1, 2018.

  1. tqd_hshl

    tqd_hshl

    Joined:
    Aug 17, 2018
    Posts:
    1
    Hey everyone,

    I'm currently experimenting with different movement types regarding player avatars in AR scenes. One thing I'm trying to set up is the movement of an object in the scene utilizing the Accelerometer. I've found easy ways to move objects in a scene using Input.acceleration, the concept isn't really that hard.

    However, as I previously discovered, moving objects in an AR scene requires you to utilize the camera's vectors, as the user can freely move the camera and directions can change by the user walking around the object.

    Trying to combine those two concepts ends up looking like this:

    Code (CSharp):
    1.  
    2.         var camRight = Camera.main.transform.right;
    3.         camRight.y = 0.0f;
    4.         camRight.Normalize();
    5.  
    6.         var camFor = Camera.main.transform.forward;
    7.         camFor.y = 0.0f;
    8.         camFor.Normalize();
    9.  
    10.         float accelerationX = Input.acceleration.x;
    11.         Vector3 rightLeftMove = Vector3.zero;
    12.         if(accelerationX >= 0.3 || accelerationX <= -0.3)
    13.         {
    14.             rightLeftMove = camRight * -accelerationX;
    15.         }
    16.  
    17.         float accelerationY = Input.acceleration.y + 0.45f;
    18.         Vector3 upDownMove = Vector3.zero;
    19.         if(accelerationY >= 0.3 || accelerationY < 0.3)
    20.         {
    21.             upDownMove = camFor * -accelerationY;
    22.         }
    23.  
    24.         // Creates movement vector
    25.         Vector3 moveVector = rightLeftMove + upDownMove;
    26.  
    27.         if(moveVector != Vector3.zero)
    28.         {
    29.             isMoving = true;
    30.             Vector3 translationVector = moveVector * moveSpeed * Time.deltaTime;
    31.             transform.Translate(translationVector, Space.World);
    32.         }
    33.         else
    34.         {
    35.             isMoving = false;
    36.         }
    I reversed the input so tilting your camera to the right actually makes the object move right, allowing you to keep it in your cameras field of view. However, the results are slow and hard to handle. I'm playing with some of these values trying to create a device rotation in which the user doesn't move the object, but isMoving never prints as false.

    Also, in order to move the object backwards, the device needs to be tilted close to parallel to the floor - however, since I'm using the camera's vectors, the movement on the plane becomes really slow and often times doesn't work correctly.

    Does anyone know what might be a better approach than to use the camera's vectors for transforming the object relative to the users position?

    I've been sitting over this for days, maybe some fresh perspective is all I need for new ideas. Any help is appreciated!