Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question SteamVR action only shows fromsource as 'any'

Discussion in 'VR' started by Cronnoc, Nov 5, 2021.

  1. Cronnoc

    Cronnoc

    Joined:
    Jul 6, 2018
    Posts:
    18
    Hello, I am having a problem. I have an interaction system in my VR game and it works fine, however if I have one controller hovering over the interactable, and then I press the button on the other controller, it still interacts with it. I know this is because I have the actionset mirrored as oppose to per hand, however I was hoping to be able to see what the input source is and then compare it to the controller's source.

    Code (CSharp):
    1. void Awake()
    2. {
    3.     SteamVR_Actions.menu_Interact.onStateDown += Interact;
    4.  
    5.     inputSource = GetComponentInParent<PlayerController
    6.         ().playerHand.GetComponent<SteamVR_Behaviour_Pose>().inputSource;
    7. }
    8.  
    9. void Interact(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
    10. {
    11.     if (!_canInteract)
    12.         return;
    13.  
    14.     if (fromSource == inputSource && _targetObject != null)
    15.         _targetObject.Interact();
    16. }
    Unfortunately, the 'fromSource' variable is always set to SteamVR_Input_Sources.Any. Is there a way for me to see what input source is actually trigger the action or do I simply need to set the menu actionset to per hand?
     
  2. Cronnoc

    Cronnoc

    Joined:
    Jul 6, 2018
    Posts:
    18
    So, I solved it. Turns out that the SteamVR_Actions.menu_Interact.onStateDown checks InputSource.any by default. It isn't an issue with per hand actions because there is only one way that the action can occur. With mirrored actions you need to use the AddOnStateDownListener function. Here is my updated (working) code.

    Code (CSharp):
    1.  
    2. void Awake()
    3. {
    4.     inputSource = GetComponentInParent<PlayerController>().playerHand.GetComponent<SteamVR_Behaviour_Pose>().inputSource;
    5.     SteamVR_Actions.menu_Interact.AddOnStateDownListener(Interact, inputSource);
    6. }
    7. void Interact(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
    8. {
    9.     if (!_canInteract)
    10.         return;
    11.     if (fromSource == inputSource && _targetObject != null)
    12.         _targetObject.Interact();
    13. }