TriggerForFarNearSwitch.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using EZXR.Glass.Core;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace EZXR.Glass.Inputs
  6. {
  7. [ScriptExecutionOrder(-47)]
  8. public class TriggerForFarNearSwitch : MonoBehaviour
  9. {
  10. //检测Trigger的长度
  11. public static float length = 0.24f;
  12. HandInfo handInfo;
  13. private void Awake()
  14. {
  15. //transform.localScale = Vector3.one * radius * 2;
  16. }
  17. // Start is called before the first frame update
  18. void Start()
  19. {
  20. }
  21. // Update is called once per frame
  22. void Update()
  23. {
  24. if (handInfo != null && handInfo.Exist)
  25. {
  26. Pose pose = handInfo.GetJointData(HandJointType.Palm);
  27. transform.position = pose.position;
  28. transform.rotation = pose.rotation; ;
  29. }
  30. }
  31. private void OnDisable()
  32. {
  33. handInfo.PreCloseContactingTarget = null;
  34. handInfo.Last_PreCloseContactingTarget = null;
  35. }
  36. public void SetUp(Transform handRoot)
  37. {
  38. handInfo = handRoot.GetComponent<HandInfo>();
  39. }
  40. private void OnTriggerStay(Collider other)
  41. {
  42. if (other.gameObject.layer != gameObject.layer)
  43. {
  44. if (handInfo != null)
  45. {
  46. //“当用户的手是up状态”且“当前手没有进入其他Trigger”时
  47. if (!handInfo.isPinching && !handInfo.IsCloseContacting)
  48. {
  49. if (other.tag == "SpatialObject" || other.tag == "SpatialUI" || other.tag == "SpatialHandler")
  50. {
  51. handInfo.preCloseContacting = true;
  52. if (handInfo.PreCloseContactingTarget == null || Vector3.Distance(transform.position, other.transform.position) < Vector3.Distance(transform.position, handInfo.PreCloseContactingTarget.position))
  53. {
  54. //handInfo.Last_PreCloseContactingTarget = handInfo.PreCloseContactingTarget;
  55. handInfo.PreCloseContactingTarget = other.transform;
  56. }
  57. }
  58. }
  59. }
  60. }
  61. }
  62. private void OnTriggerExit(Collider other)
  63. {
  64. if (other.gameObject.layer != gameObject.layer)
  65. {
  66. if (handInfo != null)
  67. {
  68. //“当用户的手是up状态”且“当前手没有进入其他Trigger”时
  69. if (!handInfo.isPinching && !handInfo.IsCloseContacting)
  70. {
  71. if (other.tag == "SpatialObject" || other.tag == "SpatialUI" || other.tag == "SpatialHandler")
  72. {
  73. if (handInfo.PreCloseContactingTarget == other.transform)
  74. {
  75. //handInfo.Last_PreCloseContactingTarget = handInfo.PreCloseContactingTarget;
  76. handInfo.PreCloseContactingTarget = null;
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }