MegaPathFollow.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. 
  2. using UnityEngine;
  3. [ExecuteInEditMode]
  4. public class MegaPathFollow : MonoBehaviour
  5. {
  6. public float tangentDist = 0.01f; // how far it looks ahead or behind to calc rotation
  7. public float alpha = 0.0f; // how far along curve as a percent
  8. public float speed = 0.0f; // how fast it moves
  9. public bool rot = false; // check if you want to change rotation
  10. public float time = 0.0f; // how long to take to travel whole shape (system checks UseDistance then time then speed for which method it chooses, set non used to 0)
  11. public float ctime = 0.0f; // current time for time animation
  12. public int curve = 0; // curve to use in shape
  13. public MegaShape target; // Shape to follow
  14. public float distance = 0.0f; // distance along shape
  15. public bool animate = false; // automatically moves the object
  16. public bool UseDistance = true; // use distance method
  17. public bool addtwist = false;
  18. public Vector3 offset = Vector3.zero;
  19. public Vector3 rotate = Vector3.zero;
  20. public MegaRepeatMode loopmode = MegaRepeatMode.Loop;
  21. public void SetPos(float a)
  22. {
  23. if ( target != null )
  24. {
  25. float twist = 0.0f;
  26. switch ( loopmode )
  27. {
  28. case MegaRepeatMode.Clamp: a = Mathf.Clamp01(a); break;
  29. case MegaRepeatMode.Loop: a = Mathf.Repeat(a, 1.0f); break;
  30. case MegaRepeatMode.PingPong: a = Mathf.PingPong(a, 1.0f); break;
  31. }
  32. Vector3 off = Vector3.zero;
  33. Vector3 pos = target.InterpCurve3D(curve, a, target.normalizedInterp, ref twist);
  34. if ( rot )
  35. {
  36. float ta = tangentDist / target.GetCurveLength(curve);
  37. Vector3 pos1 = target.InterpCurve3D(curve, a + ta, target.normalizedInterp);
  38. Vector3 rt = rotate;
  39. Quaternion tq = Quaternion.Euler(0.0f, 0.0f, twist);
  40. Quaternion er = Quaternion.Euler(rt);
  41. if ( addtwist )
  42. er = tq * er;
  43. Vector3 dir = pos1 - pos;
  44. Quaternion r = Quaternion.LookRotation(dir); //pos1 - pos); //transform.LookAt(target.transform.TransformPoint(target.InterpCurve3D(curve, a + ta, target.normalizedInterp)));
  45. // Calc offset vector
  46. Matrix4x4 tm = Matrix4x4.TRS(Vector3.zero, r * er, Vector3.one);
  47. off = tm.MultiplyPoint3x4(offset);
  48. transform.localRotation = r * er;
  49. }
  50. transform.position = target.transform.TransformPoint(pos - off); // + offset;
  51. }
  52. }
  53. public void SetPosFomDist(float dist)
  54. {
  55. if ( target != null )
  56. {
  57. //float a = Mathf.Repeat(dist / target.GetCurveLength(curve), 1.0f);
  58. float a = dist / target.GetCurveLength(curve);
  59. float twist = 0.0f;
  60. switch ( loopmode )
  61. {
  62. case MegaRepeatMode.Clamp: a = Mathf.Clamp01(a); break;
  63. case MegaRepeatMode.Loop: a = Mathf.Repeat(a, 1.0f); break;
  64. case MegaRepeatMode.PingPong: a = Mathf.PingPong(a, 1.0f); break;
  65. }
  66. Vector3 off = Vector3.zero;
  67. Vector3 pos = target.InterpCurve3D(curve, a, target.normalizedInterp, ref twist);
  68. if ( rot )
  69. {
  70. float ta = tangentDist / target.GetCurveLength(curve);
  71. Vector3 pos1 = target.InterpCurve3D(curve, a + ta, target.normalizedInterp);
  72. Vector3 rt = rotate;
  73. Quaternion tq = Quaternion.Euler(0.0f, 0.0f, twist);
  74. Quaternion er = Quaternion.Euler(rt); //otate);
  75. if ( addtwist )
  76. er = tq * er;
  77. Vector3 dir = pos1 - pos;
  78. Quaternion r = Quaternion.LookRotation(dir); //pos1 - pos); //transform.LookAt(target.transform.TransformPoint(target.InterpCurve3D(curve, a + ta, target.normalizedInterp)));
  79. // Calc offset vector
  80. Matrix4x4 tm = Matrix4x4.TRS(Vector3.zero, r * er, Vector3.one);
  81. off = tm.MultiplyPoint3x4(offset);
  82. transform.localRotation = r * er;
  83. }
  84. transform.position = target.transform.TransformPoint(pos - off); // + offset;
  85. }
  86. }
  87. public void Start()
  88. {
  89. ctime = 0.0f;
  90. curve = 0;
  91. }
  92. void Update()
  93. {
  94. if ( animate )
  95. {
  96. if ( UseDistance )
  97. distance += speed * Time.deltaTime;
  98. else
  99. {
  100. if ( time > 0.0f )
  101. {
  102. ctime += Time.deltaTime;
  103. if ( ctime > time )
  104. ctime = 0.0f;
  105. alpha = (ctime / time) * 100.0f;
  106. }
  107. else
  108. {
  109. if ( speed != 0.0f )
  110. {
  111. alpha += speed * Time.deltaTime;
  112. if ( alpha > 100.0f )
  113. alpha = 0.0f;
  114. else
  115. {
  116. if ( alpha < 0.0f )
  117. alpha = 100.0f;
  118. }
  119. }
  120. }
  121. }
  122. }
  123. if ( UseDistance )
  124. SetPosFomDist(distance);
  125. else
  126. SetPos(alpha * 0.01f);
  127. }
  128. }