ButtonMod.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using Valve.VR;
  6. public class ButtonMod : CustomInteractible
  7. {
  8. public float distanseToPress; //button press reach distance
  9. [Range(.1f,1f)]
  10. public float DistanceMultiply=.1f; //button sensetivity slowdown
  11. public Transform MoveObject; //movable button object
  12. public UnityEvent ButtonDown, ButtonUp, ButtonUpdate; // events
  13. private Color32 oldColor;
  14. float StartButtonPosition; //tech variable, assigned at start of pressed button
  15. bool press; //button check, to ButtonDown call 1 time
  16. void Awake()
  17. {
  18. StartButtonPosition = MoveObject.localPosition.z;
  19. oldColor = GetComponentInChildren<MeshRenderer>().material.color;
  20. }
  21. void GrabStart(CustomHand hand)
  22. {
  23. SetInteractibleVariable(hand);
  24. hand.SkeletonUpdate();
  25. hand.grabType = CustomHand.GrabType.Select;
  26. Grab.Invoke ();
  27. }
  28. void GrabUpdate(CustomHand hand)
  29. {
  30. if ((rightHand || leftHand) && GetMyGrabPoserTransform(hand))
  31. {
  32. hand.SkeletonUpdate();
  33. GetComponentInChildren<MeshRenderer>().material.color = Color.grey;
  34. float tempDistance = Mathf.Clamp(StartButtonPosition-(StartButtonPosition-transform.InverseTransformPoint(hand.PivotPoser.position).z)*DistanceMultiply, StartButtonPosition, distanseToPress);
  35. if (tempDistance >= distanseToPress)
  36. {
  37. GetComponentInChildren<MeshRenderer>().material.color = Color.red;
  38. if (!press)
  39. {
  40. ButtonDown.Invoke();
  41. SteamVR_Action_Vibration vibration = SteamVR_Input.GetVibrationAction("Haptic");
  42. if(rightHand){
  43. vibration.Execute(0f, 0.1f, 50f, 1f, SteamVR_Input_Sources.RightHand);
  44. }
  45. else{
  46. vibration.Execute(0f, 0.1f, 50f, 1f, SteamVR_Input_Sources.LeftHand);
  47. }
  48. }
  49. press = true;
  50. ButtonUpdate.Invoke();
  51. }
  52. else
  53. {
  54. if (press)
  55. {
  56. ButtonUp.Invoke();
  57. }
  58. press = false;
  59. }
  60. MoveObject.localPosition = new Vector3(0, 0, tempDistance);
  61. MoveObject.rotation = Quaternion.LookRotation(GetMyGrabPoserTransform(hand).forward, hand.PivotPoser.up);
  62. hand.GrabUpdateCustom();
  63. }
  64. }
  65. void GrabEnd(CustomHand hand)
  66. {
  67. //if ((rightHand || leftHand) && GetMyGrabPoserTransform(hand))
  68. //{
  69. MoveObject.localPosition = new Vector3(0, 0, StartButtonPosition);
  70. DettachHand(hand);
  71. GetComponentInChildren<MeshRenderer>().material.color = oldColor;
  72. //}
  73. ReleaseHand.Invoke ();
  74. }
  75. }