DampingJoint.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using UnityEngine;
  2. using System.Collections;
  3. public class DampingJoint : MonoBehaviour {
  4. public Transform from;
  5. public Transform to;
  6. public Transform Launcher;
  7. public Transform Reciever;
  8. public SpriteSheetIndexV ssv;
  9. public float unitLength;
  10. public bool autoMode = false;
  11. public enum MoveType { Time, Speed };
  12. public enum AccMode { Average, X3 };
  13. public enum STATE
  14. {
  15. NONE,
  16. FLY,
  17. ATTACHED,
  18. BACK,
  19. }
  20. public STATE state {get; private set;}
  21. private float flyto;
  22. private float keep;
  23. private float flyback;
  24. // Use this for initialization
  25. void Start () {
  26. state = STATE.NONE;
  27. }
  28. // Update is called once per frame
  29. void Update () {
  30. if (ssv != null)
  31. {
  32. float l = Vector3.Distance(from.transform.position, to.transform.position);
  33. ssv.SetRepeat(l / unitLength);
  34. }
  35. if (!autoMode) return;
  36. if (state == STATE.NONE)
  37. {
  38. return;
  39. }
  40. if(state == STATE.ATTACHED)
  41. {
  42. to.position = Reciever.position;
  43. }
  44. from.position = Launcher.position;
  45. }
  46. public void Init(Transform launcher)
  47. {
  48. autoMode = false;
  49. Reciever = null;
  50. //Launcher = launcher;
  51. // transform.position = launcher.position;
  52. //transform.rotation = launcher.rotation;
  53. //from.transform.position = launcher.position;
  54. //from.transform.rotation = launcher.rotation;
  55. }
  56. public void Sync(Vector3 pos)
  57. {
  58. to.position = pos;
  59. }
  60. public void Sync(Vector3 pos1, Vector3 pos2)
  61. {
  62. from.position = pos1;
  63. to.position = pos2;
  64. }
  65. public void Play(Transform launcher, Transform reciever, float flyto, float keep, float flyback)
  66. {
  67. Play(launcher, reciever.position, flyto, keep, flyback);
  68. Reciever = reciever;
  69. }
  70. public void Play(Transform launcher, Vector3 position, float flyto, float keep, float flyback)
  71. {
  72. autoMode = true;
  73. Reciever = null;
  74. Launcher = launcher;
  75. this.flyto = flyto;
  76. this.keep = keep;
  77. this.flyback = flyback;
  78. transform.position = launcher.position;
  79. transform.rotation = launcher.rotation;
  80. if (this.flyto > 0)
  81. {
  82. state = STATE.FLY;
  83. StartCoroutine(Translation(to, to.position, position, flyto, MoveType.Time, AccMode.X3, STATE.FLY));
  84. }
  85. else
  86. {
  87. state = STATE.ATTACHED;
  88. }
  89. }
  90. public void Hook(Transform reciever)
  91. {
  92. Reciever = reciever;
  93. state = STATE.ATTACHED;
  94. }
  95. public void Back()
  96. {
  97. state = STATE.BACK;
  98. Reciever = from;
  99. StartCoroutine(Translation(to, to.position, Reciever.position, flyback, MoveType.Time, AccMode.X3, STATE.BACK));
  100. }
  101. public void Back(float flyback)
  102. {
  103. this.flyback = flyback;
  104. Back();
  105. }
  106. public IEnumerator Translation(Transform thisTransform, Vector3 startPos, Vector3 endPos, float value, MoveType moveType,
  107. AccMode accMode, STATE check)
  108. {
  109. var rate = (moveType == MoveType.Time) ? 1.0f / value : 1.0f / Vector3.Distance(startPos, endPos) * value;
  110. float t = 0.0f;
  111. while (t < (float)1.0f)
  112. {
  113. if (check != state) break;
  114. //if(!target) break;
  115. t += Time.deltaTime * rate;
  116. float res = t;
  117. if (accMode == AccMode.X3)
  118. {
  119. res = t * t * t;
  120. }
  121. if (Reciever)
  122. {
  123. thisTransform.position = Vector3.Lerp(startPos, Reciever.position, res);
  124. endPos = Reciever.position;
  125. }
  126. else
  127. {
  128. thisTransform.position = Vector3.Lerp(startPos, endPos, res);
  129. }
  130. yield return 0;
  131. }
  132. if (Reciever && state == STATE.FLY)
  133. {
  134. state = STATE.ATTACHED;
  135. }
  136. }
  137. public IEnumerator Translation(Transform thisTransform, Vector3 endPos, float value, MoveType moveType,
  138. AccMode accMode, STATE check)
  139. {
  140. yield return Translation(thisTransform, thisTransform.position, endPos, value, moveType, accMode, check);
  141. }
  142. }