MegaTrainFollow.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. [System.Serializable]
  4. public class MegaCarriage
  5. {
  6. //public GameObject coupling;
  7. public GameObject carriage;
  8. public GameObject bogey1;
  9. public GameObject bogey2;
  10. public Vector3 bogey1Offset = Vector3.zero;
  11. public Vector3 bogey2Offset = Vector3.zero;
  12. public Vector3 carriageOffset = Vector3.zero;
  13. public float bogeyoff = 0.0f;
  14. public float length = 0.0f;
  15. public Vector3 rot = Vector3.zero;
  16. public Vector3 bogey1Rot = Vector3.zero;
  17. public Vector3 bogey2Rot = Vector3.zero;
  18. //public float aheadDist;
  19. public Vector3 b1;
  20. public Vector3 b2;
  21. public Vector3 cp;
  22. public Vector3 bp1;
  23. public Vector3 bp2;
  24. }
  25. [ExecuteInEditMode]
  26. public class MegaTrainFollow : MonoBehaviour
  27. {
  28. public MegaShape path;
  29. public int curve = 0;
  30. public List<MegaCarriage> carriages = new List<MegaCarriage>();
  31. public float distance;
  32. public float speed = 0.0f;
  33. public bool showrays = false;
  34. void Update()
  35. {
  36. if ( Application.isPlaying )
  37. distance += speed * Time.deltaTime;
  38. if ( path )
  39. {
  40. float cdist = distance;
  41. MegaSpline spline = path.splines[curve];
  42. // Start at front of train and work backwards, first obj in list is head of train
  43. for ( int i = 0; i < carriages.Count; i++ )
  44. {
  45. float alpha = cdist / spline.length;
  46. MegaCarriage car = carriages[i];
  47. float tw = 0.0f;
  48. float tw1 = 0.0f;
  49. car.b1 = path.transform.TransformPoint(path.InterpCurve3D(curve, alpha, true, ref tw));
  50. float d = cdist - car.length; //bogey2Offset.z;
  51. float alpha1 = d / spline.length;
  52. car.b2 = path.transform.TransformPoint(path.InterpCurve3D(curve, alpha1, true, ref tw1));
  53. car.cp = (car.b1 + car.b2) * 0.5f;
  54. //if ( showrays )
  55. // Debug.DrawLine(b1, b2);
  56. // Carriage is positioned half way between the bogeys
  57. if ( car.carriage )
  58. {
  59. // Offset can adjust this, so lerp from b1 to b2 based on offset
  60. //Vector3 cp = (b1 + b2) * 0.5f;
  61. //float twist = (tw + tw1) * 0.5f;
  62. //Quaternion trot = Quaternion.Euler(0.0f, twist, 0.0f);
  63. car.carriage.transform.position = car.cp + car.carriageOffset;
  64. Quaternion erot = Quaternion.Euler(car.rot);
  65. Quaternion rot = Quaternion.LookRotation(car.b1 - car.b2);
  66. car.carriage.transform.rotation = rot * erot; // * trot;
  67. }
  68. if ( car.bogey1 && car.carriage )
  69. {
  70. car.bogey1.transform.position = car.carriage.transform.localToWorldMatrix.MultiplyPoint(car.bogey1Offset); //b1;
  71. Quaternion erot = Quaternion.Euler(car.bogey1Rot);
  72. float a = alpha - (car.bogeyoff / spline.length);
  73. car.bp1 = path.transform.TransformPoint(path.InterpCurve3D(curve, a, true));
  74. Vector3 p2 = path.transform.TransformPoint(path.InterpCurve3D(curve, a + 0.0001f, true));
  75. Quaternion rot = Quaternion.LookRotation(p2 - car.bp1);
  76. car.bogey1.transform.rotation = rot * erot;
  77. //if ( showrays )
  78. // Debug.DrawLine(car.cp, car.bp1, Color.red);
  79. }
  80. if ( car.bogey2 && car.carriage )
  81. {
  82. car.bogey2.transform.position = car.carriage.transform.localToWorldMatrix.MultiplyPoint(car.bogey2Offset); //b1;
  83. Quaternion erot = Quaternion.Euler(car.bogey2Rot);
  84. //float tw = 0.0f;
  85. float a = alpha1 + (car.bogeyoff / spline.length);
  86. car.bp2 = path.transform.TransformPoint(path.InterpCurve3D(curve, a, true, ref tw));
  87. Vector3 p2 = path.transform.TransformPoint(path.InterpCurve3D(curve, a + 0.0001f, true));
  88. Quaternion rot = Quaternion.LookRotation(p2 - car.bp2);
  89. //Quaternion trot = Quaternion.Euler(-tw, 0.0f, 0.0f);
  90. car.bogey2.transform.rotation = rot * erot;// * trot;
  91. //if ( showrays )
  92. // Debug.DrawLine(cp, p1, Color.blue);
  93. }
  94. // Then move back a bit for next car
  95. cdist -= car.length;
  96. }
  97. }
  98. }
  99. }