Joystick.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Valve.VR;
  5. public class Joystick : CustomInteractible {
  6. public Transform Stick; //moving part of joystick
  7. public Vector2 value; //current position in %
  8. public Vector2 clamp=new Vector2(60,60); //limits of incline
  9. public Vector2 angle; //incline angle
  10. float handleDistance; //distance to handle/grip
  11. Quaternion rotation;
  12. public bool normalize; // square or circle limitation
  13. public bool returnToZero; // return to default position
  14. public enum TypeHandGrabRotation{
  15. free,
  16. vertical,
  17. horizontal,
  18. }
  19. public TypeHandGrabRotation typeHandGrabRotation; // hands grip behaviour
  20. // Use this for initialization
  21. void Start () {
  22. if (grabPoints!=null&&grabPoints.Count>0)
  23. handleDistance = grabPoints[0].transform.localPosition.magnitude;
  24. enabled = false;
  25. }
  26. // Update is called once per frame
  27. public void Update(){
  28. if (leftHand || rightHand)
  29. enabled = false;
  30. if (returnToZero) {
  31. value = Vector2.MoveTowards (value, Vector2.zero, Time.deltaTime);
  32. if (value == Vector2.zero)
  33. enabled = false;
  34. Stick.localRotation = Quaternion.LookRotation(Vector3.SlerpUnclamped (Vector3.SlerpUnclamped (new Vector3 (-1, -1, 1), new Vector3 (-1, 1, 1), value.x*clamp.x/90+.5f),Vector3.SlerpUnclamped (new Vector3 (1, -1, 1), new Vector3 (1, 1, 1), value.x*clamp.x/90+.5f),value.y*clamp.y/90+.5f),Vector3.up);
  35. Transform tempPoser = grabPoints[0].transform;
  36. if (typeHandGrabRotation == TypeHandGrabRotation.vertical) {
  37. tempPoser.rotation = Quaternion.LookRotation (Stick.forward, Stick.up);
  38. } else {
  39. if (typeHandGrabRotation == TypeHandGrabRotation.horizontal) {
  40. tempPoser.rotation = Quaternion.LookRotation (Stick.up, Stick.forward);
  41. }
  42. }
  43. tempPoser.position = Stick.TransformPoint(new Vector3(0,0, handleDistance));
  44. }
  45. }
  46. public void GrabStart(CustomHand hand){
  47. SetInteractibleVariable (hand);
  48. hand.SkeletonUpdate ();
  49. Grab.Invoke ();
  50. }
  51. public void GrabUpdate(CustomHand hand){
  52. Transform tempPoser = GetMyGrabPoserTransform (hand);
  53. tempPoser.position = hand.PivotPoser.position;
  54. tempPoser.localPosition = new Vector3 (tempPoser.localPosition.x, tempPoser.localPosition.y, Mathf.Abs(tempPoser.localPosition.z));
  55. angle.x = Vector2.SignedAngle(new Vector2(tempPoser.localPosition.y,tempPoser.localPosition.z),Vector2.up);
  56. angle.y = Vector2.SignedAngle(new Vector2(tempPoser.localPosition.x,tempPoser.localPosition.z),Vector2.up);
  57. angle = new Vector2 (Mathf.Clamp (angle.x, -clamp.x, clamp.x), Mathf.Clamp (angle.y, -clamp.y, clamp.y));
  58. value = new Vector2 (angle.x / (clamp.x + Mathf.Epsilon), angle.y / (clamp.y + Mathf.Epsilon));
  59. if (normalize)
  60. value=Vector2.ClampMagnitude(value,1);
  61. Stick.localRotation = Quaternion.LookRotation(Vector3.SlerpUnclamped (Vector3.SlerpUnclamped (new Vector3 (-1, -1, 1), new Vector3 (-1, 1, 1), value.x*clamp.x/90+.5f),Vector3.SlerpUnclamped (new Vector3 (1, -1, 1), new Vector3 (1, 1, 1), value.x*clamp.x/90+.5f),value.y*clamp.y/90+.5f),Vector3.up);
  62. if (typeHandGrabRotation == TypeHandGrabRotation.vertical) {
  63. tempPoser.rotation = Quaternion.LookRotation (Stick.forward, hand.PivotPoser.up);
  64. } else {
  65. if (typeHandGrabRotation == TypeHandGrabRotation.horizontal) {
  66. tempPoser.rotation = Quaternion.LookRotation (Stick.up, hand.PivotPoser.up);
  67. } else {
  68. tempPoser.rotation = hand.PivotPoser.rotation;
  69. }
  70. }
  71. tempPoser.position = Stick.TransformPoint(new Vector3(0,0, handleDistance));
  72. }
  73. public void GrabEnd(CustomHand hand){
  74. DettachHand (hand);
  75. if (returnToZero) {
  76. enabled = true;
  77. }
  78. ReleaseHand.Invoke ();
  79. }
  80. }