PathFollow.cs 4.2 KB

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