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

[Resolved] How should i detect if the device is left or right?

Discussion in 'Input System' started by Extrys, Feb 20, 2020.

  1. Extrys

    Extrys

    Joined:
    Oct 25, 2017
    Posts:
    343
    Hello everyone, So how should i evade this
    upload_2020-2-20_20-6-9.png

    This just makes me having more events and makes dificut to manage the input for left and right systems

    So for exame i have 2 inventory viewers, one for left hand and another for right hand
    how i should detect if the controller who is presing is left or right or even both

    i want to have just One "Inventory" action, and not 2 actions for each controller
    so its not escalable and make the input reception just more complexand a lot more input to change
     

    Attached Files:

  2. Extrys

    Extrys

    Joined:
    Oct 25, 2017
    Posts:
    343
    Ok i found how to do it,
    just comparing with context

    using UnityEngine.InputSystem;
    using UnityEngine.InputSystem.XR;

    ctx.control.device == XRInputDevice.LeftHand/RightHand
     
  3. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Alternatively, you can check "usages" on the devices. LeftHand and RightHand are just string tags applies as usages to the device.

    Code (CSharp):
    1. var device = context.control.device;
    2. if (device.usages.Contains(CommonUsages.LeftHand))
    3.     Debug.Log("Left hand!");
    4. else if (device.usages.Contains(CommonUsages.RightHand))
    5.     Debug.Log("Right hand!");
    Probably would be nice to have some property on XRController to simplify this.
     
    esanuriana08 likes this.
  4. Extrys

    Extrys

    Joined:
    Oct 25, 2017
    Posts:
    343
    Thanks for your reply, just a question, is not "ctx.control.device == XRInputDevice.LeftHand/RightHand"
    Better than using ".Contains(sring)" since you are comparing strings?
     
  5. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    This is what XRController.leftHand does

    Code (CSharp):
    1.         public static XRController leftHand => InputSystem.GetDevice<XRController>(CommonUsages.LeftHand);
    2.  
    I.e. it looks up the device by string tag. Directly checking the already supplied device for its tags is recommended.
     
    Extrys likes this.
  6. Extrys

    Extrys

    Joined:
    Oct 25, 2017
    Posts:
    343
    Ohhhh Good to know!
    that code was assembled so its hidden for me,
    thanks a lot for this info.
     
    Last edited: Mar 18, 2020
  7. EoinDTCo

    EoinDTCo

    Joined:
    Apr 15, 2021
    Posts:
    1
    I have been trying to use this code to detect if the XR controller pointing at the object I want to interact with is the left or right hand. I cant get it to work however. Pretty much a beginner at Unity and probably missing something basic.

    The error I am getting is around the context.control.device line...

    Any help much appreciated.