Skip to content

Instantly share code, notes, and snippets.

@chakmeshma
Last active October 10, 2024 10:42
Show Gist options
  • Select an option

  • Save chakmeshma/5c5317f44c50fe07c6c4cc162716e535 to your computer and use it in GitHub Desktop.

Select an option

Save chakmeshma/5c5317f44c50fe07c6c4cc162716e535 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.XR;
public class VRInputManager : MonoBehaviour
{
public TextureManager textureManager;
public float restthreshold = 0.3f;
public float threshold = 0.7f;
private bool rightNeedRest = false;
private bool leftNeedRest = false;
private InputDevice rightController;
void Start()
{
InitializeController();
}
private void ProcessInput(Vector2 thumbstickValue)
{
if (thumbstickValue.x > threshold && !rightNeedRest)
{
textureManager.SelectNext();
rightNeedRest = true;
}
if (thumbstickValue.x < -threshold && !leftNeedRest)
{
textureManager.SelectPrevious();
leftNeedRest = true;
}
if (thumbstickValue.x < restthreshold)
{
rightNeedRest = false;
}
if (thumbstickValue.x > -restthreshold)
{
leftNeedRest = false;
}
}
void Update()
{
if (!rightController.isValid)
InitializeController();
else if (rightController.TryGetFeatureValue(CommonUsages.primary2DAxis, out Vector2 rightThumbstick))
ProcessInput(rightThumbstick);
}
private void InitializeController()
{
// Get the list of connected devices
List<InputDevice> devices = new List<InputDevice>();
InputDevices.GetDevices(devices);
// Find left and right hand controllers
foreach (var device in devices)
{
if (device.characteristics.HasFlag(InputDeviceCharacteristics.Right) &&
device.characteristics.HasFlag(InputDeviceCharacteristics.Controller))
{
rightController = device;
Debug.Log("Right controller initialized.");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment