Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Problem with VR Controller Input

Discussion in 'VR' started by kingjohnc19, Jun 28, 2020.

  1. kingjohnc19

    kingjohnc19

    Joined:
    Apr 19, 2018
    Posts:
    3
    I'm following this tutorial to create a VR game:


    I got up to 5:56 in the video. My code is identical to the code in the video, but I'll include it just in case.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR;
    5.  
    6. public class HandPresence : MonoBehaviour
    7. {
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         List<InputDevice> devices = new List<InputDevice>();
    12.         InputDeviceCharacteristics rightControllerCharacteristics = InputDeviceCharacteristics.Right | InputDeviceCharacteristics.Controller;
    13.         InputDevices.GetDevicesWithCharacteristics(rightControllerCharacteristics, devices);
    14.  
    15.         foreach (var item in devices)
    16.         {
    17.             Debug.Log(item.name + item.characteristics);
    18.         }
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.  
    25.     }
    26. }
    27.  
    When I save the code and run the game in Unity, nothing shows up in the console. Did I do something wrong? I'm using a Windows Mixed Reality headset. Thank you in advance!
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Is the code running at all? Easiest way to be sure would be to add a Debug.Log outside the foreach loop. Something like Debug.Log("Yep, HandPresence.Start is actually running") would be enough to test your assumption that this code is running.

    If it's not running, then it's probably just that you didn't attach this script to an active object in the scene.
     
  3. kingjohnc19

    kingjohnc19

    Joined:
    Apr 19, 2018
    Posts:
    3
    I've already tested this out before and It's safe to say that the code is actually running. I'm starting to suspect that my headset just doesn't work very well with the Unity XR Toolkit? I'm not really sure about anything, because this is my first time messing with VR.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I dunno. I can tell you that our VR app does work with WMR. Somewhere there is an assumption you are making that is not true; you just need to track that down (and your current script is not very helpful with that, unless perhaps you're stepping through it in the debugger). Debug.Log everything you can think of until you find that faulty assumption.
     
  5. kingjohnc19

    kingjohnc19

    Joined:
    Apr 19, 2018
    Posts:
    3
    Thank you for the help!
     
  6. cyenketswamy

    cyenketswamy

    Joined:
    Jul 24, 2015
    Posts:
    41
    Your code looks incomplete.
     
  7. jakiboy141

    jakiboy141

    Joined:
    Aug 23, 2021
    Posts:
    5
    I'm following the same tutorial and having the same issue, all its simply trying to do is print to console what items are connected to the game (being the VR controllers and headset) and nothing else. I can't figure out why it won't print, when I have identical code.
     
  8. Dark-Table

    Dark-Table

    Joined:
    Nov 25, 2008
    Posts:
    315
    When you're starting VR in the Unity Editor there frequently won't be any devices connected in frame 0, so you need to wait a bit for VR to actually initialize.

    I just tried this code out and it works:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR;
    5.  
    6. public class HandPresence : MonoBehaviour
    7. {
    8.     // Start is called before the first frame update
    9.     IEnumerator Start() {
    10.         List<InputDevice> devices = new List<InputDevice>();
    11.         InputDeviceCharacteristics rightControllerCharacteristics = InputDeviceCharacteristics.Right | InputDeviceCharacteristics.Controller;
    12.  
    13.         while (devices.Count == 0) {
    14.             // check for devices every frame until you find one.
    15.             yield return null;
    16.             InputDevices.GetDevicesWithCharacteristics(rightControllerCharacteristics, devices);
    17.         }
    18.  
    19.         foreach (var item in devices)
    20.         {
    21.             Debug.Log(item.name + item.characteristics);
    22.         }
    23.     }
    24. }
    25.  
     
    JuliadeBerga likes this.
  9. unity_C33D766297F677B0775E

    unity_C33D766297F677B0775E

    Joined:
    Apr 20, 2023
    Posts:
    2