Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.
Options

MixedReality Motion Controllers - Best practises?

Hi Finally found a quite clean way to get input from the Motion Controllers while using Unity.
Would be nice with even more tips about this and best practices.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
using UnityEngine.XR.WSA.Input;

public class MixedReality_MotionControllerHandler : MonoBehaviour
{
bool _graspToogle = false, _grasping = false;

public void OnEnable()
{
    InteractionManager.InteractionSourcePressed += InteractionManager_InteractionSourcePressed;
    InteractionManager.InteractionSourceUpdated += InteractionManager_InteractionSourceUpdated;
    InteractionManager.InteractionSourceReleased += InteractionManager_InteractionSourceReleased;
}

public void OnDisable()
{
    InteractionManager.InteractionSourcePressed -= InteractionManager_InteractionSourcePressed;
    InteractionManager.InteractionSourceUpdated -= InteractionManager_InteractionSourceUpdated;
    InteractionManager.InteractionSourceReleased -= InteractionManager_InteractionSourceReleased;
}

private void InteractionManager_InteractionSourcePressed(InteractionSourcePressedEventArgs obj)
{

    Vector2 _thumbstickPosition = obj.state.thumbstickPosition;

    switch (obj.pressType)
    {
        case InteractionSourcePressType.None:
            break;
        case InteractionSourcePressType.Select:
            break;
        case InteractionSourcePressType.Menu:
            break;
        case InteractionSourcePressType.Grasp:
            if(obj.state.source.handedness == InteractionSourceHandedness.Left) // if left hand is being used
            {
                _grasping = true;
                _graspToogle = !_graspToogle;
                GraspToogled(_graspToogle);
            }
            break;
        case InteractionSourcePressType.Touchpad:
            break;
        case InteractionSourcePressType.Thumbstick:
            break;
        default:
            break;
    }

}

private void InteractionManager_InteractionSourceReleased(InteractionSourceReleasedEventArgs obj)
{
    switch (obj.pressType)
    {
        case InteractionSourcePressType.None:
            break;
        case InteractionSourcePressType.Select:
            break;
        case InteractionSourcePressType.Menu:
            break;
        case InteractionSourcePressType.Grasp:
            if (obj.state.source.handedness == InteractionSourceHandedness.Left) // if left hand is being used
            {
                _grasping = false;
            }
            break;
        case InteractionSourcePressType.Touchpad:
            break;
        case InteractionSourcePressType.Thumbstick:
            break;
        default:
            break;
    }
}

private void InteractionManager_InteractionSourceUpdated(InteractionSourceUpdatedEventArgs obj)
{
}

void GraspToogled(bool _graspToogle)
{
    if (_graspToogle)
    {

    }
    else
    {

    }
}
void Update ()
{
    if(_grasping)
    {
        //do something while we are grasping with left hand
    }

    //Position Rotation Unity way
    //Vector3 leftPosition = InputTracking.GetLocalPosition(XRNode.LeftHand); //LeftEye,RightEye,CenterEye,Head  ++ a bunch of positions
    //Quaternion leftRotation = InputTracking.GetLocalRotation(XRNode.LeftHand);


    //var interactionSourceState = InteractionManager.GetCurrentReading();

    //if (interactionSourceState.Length > 0)
    //{
    //    if (interactionSourceState[0].thumbstickPosition.x >= 0.1f)
    //    {
    //        Debug.text = "Left Stick moved Left";
    //    }
    //}
}

}

Sign In or Register to comment.