-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandPresence.cs
46 lines (43 loc) · 2.01 KB
/
HandPresence.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
public class HandPresence : MonoBehaviour
{
[SerializeField]
private InputDevice targetDevice;
// Start is called before the first frame update
void Start()
{
List<InputDevice> devices = new List<InputDevice>();
// Creates a mask filter that checks for input devices matching the characteristics right and controller, so right controller
InputDeviceCharacteristics rightControllerCharacteristics = InputDeviceCharacteristics.Right | InputDeviceCharacteristics.Controller;
// Filter through devices
InputDevices.GetDevicesWithCharacteristics(rightControllerCharacteristics, devices);
foreach (InputDevice item in devices)
{
Debug.Log(item.name + item.characteristics);
}
if (devices.Count > 0)
{
targetDevice = devices[0];
}
}
// Update is called once per frame
void Update()
{
bool successfulRead;
// The following function takes two parameters. The first is the type of input from the controller,
// like a button or trigger, while the second parameter creates a NEW variable of a corresponding
// type. Ex. bool for button and float for trigger.
successfulRead = targetDevice.TryGetFeatureValue(CommonUsages.primaryButton, out bool primaryButtonValue);
if (successfulRead && primaryButtonValue)
Debug.Log("Pressing Primary Button");
successfulRead = targetDevice.TryGetFeatureValue(CommonUsages.trigger, out float triggerValue);
if (successfulRead && triggerValue > 0.1f)
Debug.Log("Trigger Pressed " + triggerValue);
successfulRead = targetDevice.TryGetFeatureValue(CommonUsages.primary2DAxis, out Vector2 primary2DAxisValue);
if (successfulRead && primary2DAxisValue != Vector2.zero)
Debug.Log("Primary Joystick " + primary2DAxisValue);
}
}