Search Unity

How to update a custom Motion Controllers position?

Discussion in 'VR' started by cyenketswamy, May 21, 2018.

  1. cyenketswamy

    cyenketswamy

    Joined:
    Jul 24, 2015
    Posts:
    41
    I have a custom motion controller. It could be any hand held object. If i wave my hand from side to side the virtual controller doesnt follow the movement of my hands only the grip pose is changing. Any solutions to fix this will be appreciated.



    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.XR.WSA.Input;
    4.  
    5. public class MotionController : MonoBehaviour {
    6.     TextMesh txtMesh;
    7.     GameObject displayText;
    8.     Vector2 thumbstickPosition, touchPadPosition;
    9.     Pose headSetPose;
    10.     InteractionSourcePose gripPose;
    11.     Vector3 headSetPosition,gripPosition,controllerOffset;  
    12.     Quaternion headSetRotation,gripRotation;
    13.     GameObject mockController;//The virtual controller
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.  
    18.         displayText = GameObject.FindWithTag("StatusText");
    19.         txtMesh = displayText.GetComponent<TextMesh>();
    20.         txtMesh.fontStyle = FontStyle.Italic;
    21.         txtMesh.color = Color.magenta;
    22.  
    23.         mockController = GameObject.FindWithTag("MockController");
    24.         controllerOffset = new Vector3(2.5f, 2.5f, -5.0f);
    25.         //Add Events for Motion Controller
    26.         InteractionManager.InteractionSourceUpdated += InteractionManager_InteractionSourceUpdated;      
    27.         InteractionManager.GetCurrentReading();
    28.  
    29.     }
    30.    
    31.  
    32.     private void InteractionManager_InteractionSourceUpdated(InteractionSourceUpdatedEventArgs obj)
    33.     {
    34.        
    35.         if (obj.state.thumbstickPressed)
    36.         {
    37.             txtMesh.text = "Thumbstick Pressed";//ok
    38.         }
    39.         else if (obj.state.selectPressed)
    40.         {
    41.             txtMesh.text = "Trigger Pressed";//ok
    42.         }
    43.         else if (obj.state.touchpadPressed)
    44.         {
    45.             txtMesh.text = "Touchpad pressed";//ok
    46.             touchPadPosition = obj.state.touchpadPosition;
    47.             txtMesh.text = "x:" + touchPadPosition.x.ToString() + " " + "y:" + touchPadPosition.y.ToString();
    48.         }
    49.         else if (obj.state.touchpadTouched)
    50.         {
    51.             txtMesh.text = "Touchpad touched";//ok
    52.         }
    53.         else if (obj.state.menuPressed)
    54.         {
    55.             txtMesh.text = "Menu button pressed";//ok
    56.         }
    57.         else if (obj.state.grasped)
    58.         {
    59.             txtMesh.text = "Grip button pressed";//ok
    60.         }
    61.  
    62.         //NB Thumbstick neutral position inputs are not exactly zero
    63.         //thumbstickPosition = obj.state.thumbstickPosition;
    64.         //txtMesh.text = "x:"+thumbstickPosition.x.ToString() + " "+ "y:"+ thumbstickPosition.y.ToString();
    65.  
    66.         headSetPose = obj.state.headPose;
    67.         headSetPosition = headSetPose.position;
    68.         headSetRotation = headSetPose.rotation;
    69.  
    70.         gripPose = obj.state.sourcePose;
    71.  
    72.         if (gripPose.TryGetPosition(out gripPosition))
    73.         {
    74.             mockController.transform.position =headSetPosition - gripPosition+controllerOffset;//This only gives the pose not the position in space relative to the headset position
    75.         }
    76.         if (gripPose.TryGetRotation(out gripRotation, InteractionSourceNode.Grip))
    77.         {
    78.             mockController.transform.rotation = gripRotation;
    79.         }
    80.     }
    81.  
    82.     // Update is called once per frame
    83.     void Update () {
    84.        
    85.     }
    86.  
    87.     private void OnDestroy()
    88.     {
    89.         InteractionManager.InteractionSourceUpdated -= InteractionManager_InteractionSourceUpdated;      
    90.     }
    91. }
    92.  
     

    Attached Files:

  2. Unity_Wesley

    Unity_Wesley

    Unity Technologies

    Joined:
    Sep 17, 2015
    Posts:
    558
    Can you verify what data is returned from the Grip Position, you might not be getting the position.

    So what are you trying to do, I see the only position you are pulling is the grip position and applying a bunch off sets. The end goal seems very confusing, because you shouldn't need to add any offset.
     
  3. cyenketswamy

    cyenketswamy

    Joined:
    Jul 24, 2015
    Posts:
    41
    There is data being returned from the grip position but their values are quiet small (<0.2 on all axes). If i multiply the gripPosition by a scalar value then the movements are amplified. It still looks rather messy i would have expected a simple assignment
    viz: mockController.transform.position =gripPosition;
    should have been sufficient placing the object at the correct distance from the virtual view through the headset. However the controllers position relative to my head set view is too far away hence the use of the offset. Alternatively i need to reposition and rotate the mock controller in the editor. There must be a more simpler approach.
     
  4. luiziv

    luiziv

    Joined:
    Aug 31, 2016
    Posts:
    23
    How do I get the pointer pose? I don't know why grip pose is the default, and I really need the pointer pose. I'm using the new xr plugin