Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Fetch XR handed-ness from an InputAction?

Discussion in 'Input System' started by Deleted User, Jun 7, 2020.

  1. Deleted User

    Deleted User

    Guest

    I'm hoping to create two generic action maps - each that uses values such as:

    XR Controller/PrimaryButton
    XR Controller/SecondaryButton

    And then filter those input actions myself to "left hand" and "right hand" so the player can easily swap their left/right action maps.

    I'm already using a monobehaviour and listening for the mapped events, but I see no way to filter or test if something is a left hand, other than something dumb like `name.Contains("Left")` which is pretty shaky.

    Is there some sort of left/right node buried in there that I can pull out?

    Right now the only solution I can see is to make duplicate action maps -

    XR Controller (Left) / PrimaryButton
    and
    XR Controller (Right) / PrimaryButton

    and manually swap between them with my own logic. This works okay, but any time I update the input controls I have to duplicate changes in multiple places to pull this off. Which is dangerous and frustrating!
     
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Code (CSharp):
    1. void OnMyAction(InputAction.CallbackContext context)
    2. {
    3.     var device = context.control.device;
    4.     if (device.usages.Contains(CommonUsages.LeftHand))
    5.         Debug.Log("Action triggered from left hand");
    6.     else if (device.usages.Contains(CommonUsages.RightHand))
    7.         Debug.Log("Action triggered from right hand");
    8.     else
    9.         Debug.Log("Action triggered with... no hands...");
    10. }
     
    belmansjef likes this.
  3. Deleted User

    Deleted User

    Guest

    That worked great, thanks!

    I sure wish CommonUsages had an enum I could use to make a dropdown selector or something, but I can work with this. :)
     
  4. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    While not exactly the same thing, it is possible to get "common usages" for a layout which could be used for populating a dropdown.

    Code (CSharp):
    1. // Get common ussags for "XRController" layout.
    2. var usages = InputSystem.LoadLayout("XRController").commonUsages;