HandJointFollowTarget.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace NRKernal.NRExamples
  5. {
  6. public class HandJointFollowTarget : MonoBehaviour
  7. {
  8. public HandEnum handEnum;
  9. public HandJointID handJoint;
  10. public HandGesture activeByHandGesture = HandGesture.None;
  11. public bool alwaysWorldUp;
  12. public float worldUpOffset;
  13. private HandState m_RelatedHandState;
  14. private void Update()
  15. {
  16. if (!NRInput.Hands.IsRunning)
  17. return;
  18. m_RelatedHandState = NRInput.Hands.GetHandState(handEnum);
  19. UpdateJointPose();
  20. }
  21. private void UpdateJointPose()
  22. {
  23. if (m_RelatedHandState == null || !m_RelatedHandState.isTracked)
  24. return;
  25. var jointPose = m_RelatedHandState.GetJointPose(handJoint);
  26. if (alwaysWorldUp)
  27. {
  28. transform.position = jointPose.position + Vector3.up * worldUpOffset;
  29. var eulerAngles = jointPose.rotation.eulerAngles;
  30. eulerAngles.x = 0f;
  31. eulerAngles.z = 0f;
  32. transform.eulerAngles = eulerAngles;
  33. }
  34. else
  35. {
  36. transform.position = jointPose.position;
  37. transform.rotation = jointPose.rotation;
  38. }
  39. }
  40. private bool ShouldActive()
  41. {
  42. if (m_RelatedHandState == null || !m_RelatedHandState.isTracked)
  43. return false;
  44. if (activeByHandGesture == HandGesture.None)
  45. return true;
  46. if (m_RelatedHandState.currentGesture == activeByHandGesture)
  47. return true;
  48. return false;
  49. }
  50. }
  51. }