Search Unity

How To Get Acceleration From Motion Controller?

Discussion in 'VR' started by jendrikb, Apr 14, 2019.

  1. jendrikb

    jendrikb

    Joined:
    Sep 22, 2017
    Posts:
    12
    Hi,
    I´m trying to get the acceleration from the motion controllers. I´m using only the XR API from Unity, because I want to keep it functional for all VR headsets.

    With the following code I get just 0,0 back. Does anybody know how to archieve the correct value?

    Code (CSharp):
    1. void Start () {
    2.         nodeStates = new List<XRNodeState>();
    3.         List<XRNodeState> tempStates = new List<XRNodeState>();
    4.         InputTracking.GetNodeStates(tempStates);
    5.  
    6.        
    7.         foreach (XRNodeState state in tempStates)
    8.         {
    9.             if (state.nodeType == XRNode.LeftHand || state.nodeType == XRNode.RightHand)
    10.                 nodeStates.Add(state);
    11.         }
    12.  
    13.         InputDevice right = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
    14.     }
    15.      void Update () {
    16.  
    17.         foreach(XRNodeState node in nodeStates)
    18.         {
    19.             Vector3 acc = new Vector3();
    20.             if(node.TryGetAcceleration(out acc))
    21.                 print(node.nodeType + " " + acc);
    22.         }
    23.     }
     
  2. jendrikb

    jendrikb

    Joined:
    Sep 22, 2017
    Posts:
    12
    Unfortunally there seems not to be a solution.
    I´m using now a script from another user, he used for throwing objects in VR. See here.

    My script is used for movement of the player, by accelerate / shaking the motion controllers. Here is my solution, if anybody needs it.

    Code (CSharp):
    1.  
    2. public class Accelerometer : AController {
    3.  
    4.     private MeasureRotationVelocity right;
    5.     private MeasureRotationVelocity left;
    6.     private Quaternion previousRotation;
    7.  
    8.     public float movementSpeed;
    9.  
    10.  
    11.     void Start () {
    12.         GameObject rightController = GameObject.FindGameObjectWithTag("RightHand");
    13.         GameObject leftController = GameObject.FindGameObjectWithTag("LeftHand");
    14.  
    15.         right = rightController.AddComponent<MeasureRotationVelocity>();
    16.         left = leftController.AddComponent<MeasureRotationVelocity>();
    17.  
    18.         right.movementSpeed = movementSpeed;
    19.         left.movementSpeed = movementSpeed;
    20.     }
    21.      void Update () {
    22.  
    23.         float velocity = right.velocity + left.velocity;
    24.  
    25.         if (velocity > 25)
    26.             velocity /= 2;
    27.  
    28.         if(velocity > 10)
    29.             MoveForward(velocity / 10);
    30.  
    31.     }
    32.  
    33.  
    34.     public class MeasureRotationVelocity : MonoBehaviour
    35.     {
    36.         private Quaternion previousRotation;
    37.  
    38.         public float movementSpeed;
    39.         public float velocity;
    40.  
    41.         void Update()
    42.         {
    43.             Quaternion deltaRotation = transform.rotation * Quaternion.Inverse(previousRotation);
    44.  
    45.             previousRotation = transform.rotation;
    46.  
    47.             float angle = 0.0f;
    48.             Vector3 axis = Vector3.zero;
    49.  
    50.             deltaRotation.ToAngleAxis(out angle, out axis);
    51.  
    52.             angle *= Mathf.Deg2Rad;
    53.  
    54.             var angularVelocity = axis * angle * (movementSpeed / Time.deltaTime);
    55.             velocity = angularVelocity.x;
    56.  
    57.             if (velocity < 0)
    58.                 velocity = velocity * -1;
    59.         }
    60.     }
    61. }
     
    Last edited: Apr 15, 2019
    Gelsongoes likes this.