Button.cs 2.4 KB

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