eye-therapy-2 / Assets / Scripts / Hands / HandController.cs
HandController.cs
Raw
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;

public class HandController : MonoBehaviour
{
    [SerializeField] private GameObject handPrefab = null;
    [SerializeField] private InputDeviceCharacteristics controllerCharacteristics = InputDeviceCharacteristics.TrackedDevice;
    [SerializeField] [Range(0f, 2f)] private float thumbMoveTime = 0.5f;


    private InputDevice device;
    private GameObject spawnedHand;
    private Animator handAnimator;
    private bool thumbCurrent = false;

    void Start()
    {
        TryInitialize();
    }

    void TryInitialize()
    {
        List<InputDevice> devices = new List<InputDevice>();
        InputDevices.GetDevicesWithCharacteristics(controllerCharacteristics, devices);
        if (devices.Count > 0)
        {
            device = devices[0];
            if (handPrefab)
            {
                spawnedHand = Instantiate(handPrefab, transform);
                handAnimator = spawnedHand.GetComponent<Animator>();
            }
        }
    }

    void Update()
    {
        if(!device.isValid)
        {
            TryInitialize();
        }
        else
        {
            UpdateHandAnimation();
        }
    }

    void UpdateHandAnimation()
    {
        // Grip
        if(device.TryGetFeatureValue(CommonUsages.grip, out float gripValue) && handAnimator.GetBool("Default"))
        {
            handAnimator.SetFloat("Grip", gripValue);
            handAnimator.SetLayerWeight(handAnimator.GetLayerIndex("Index"), gripValue);
            handAnimator.SetLayerWeight(handAnimator.GetLayerIndex("Thumb"), gripValue);
        }
        else
        {
            handAnimator.SetFloat("Grip", 0f);
            handAnimator.SetLayerWeight(handAnimator.GetLayerIndex("Index"), 0f);
            handAnimator.SetLayerWeight(handAnimator.GetLayerIndex("Thumb"), 0f);
        }

        // Trigger
        if (device.TryGetFeatureValue(CommonUsages.trigger, out float triggerValue))
        {
            handAnimator.SetFloat("Trigger", triggerValue);
        }
        else
        {
            handAnimator.SetFloat("Trigger", 0f);
        }

        // Thumb
        bool thumbNext = false;
        if (device.TryGetFeatureValue(CommonUsages.primaryTouch, out bool thumbValue)) thumbNext |= thumbValue;
        if (device.TryGetFeatureValue(CommonUsages.secondaryTouch, out bool thumbValue2)) thumbNext |= thumbValue2;
        if (device.TryGetFeatureValue(CommonUsages.primary2DAxisTouch, out bool thumbValue3)) thumbNext |= thumbValue3;
        if(thumbNext != thumbCurrent)
        {
            thumbCurrent = thumbNext;
            StopAllCoroutines();
            StartCoroutine(MoveThumb(Convert.ToSingle(thumbNext)));
        }
    }

    IEnumerator MoveThumb(float targetValue)
    {
        float startValue = handAnimator.GetFloat("Thumb");
        float t = 0;
        while(t < thumbMoveTime)
        {
            handAnimator.SetFloat("Thumb", Mathf.Lerp(startValue, targetValue, t / thumbMoveTime));
            t += Time.deltaTime;
            yield return null;
        }
        handAnimator.SetFloat("Thumb", targetValue);
    }
}