You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.5 KiB

7 years ago
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using DentedPixel;
  5. public class PathSplineEndlessCS : MonoBehaviour {
  6. public GameObject trackTrailRenderers;
  7. public GameObject car;
  8. public GameObject carInternal;
  9. public GameObject[] cubes;
  10. private int cubesIter;
  11. public GameObject[] trees;
  12. private int treesIter;
  13. public float randomIterWidth = 0.1f;
  14. private LTSpline track;
  15. private List<Vector3> trackPts = new List<Vector3>();
  16. private int zIter = 0;
  17. private float carIter = 0f;
  18. private float carAdd;
  19. private int trackMaxItems = 15;
  20. private int trackIter = 1;
  21. private float pushTrackAhead = 0f;
  22. private float randomIter = 0f;
  23. void Start () {
  24. // Setup initial track points
  25. for(int i = 0; i < 4; i++){
  26. addRandomTrackPoint();
  27. }
  28. refreshSpline();
  29. // Animate in track ahead of the car
  30. LeanTween.value(gameObject, 0, 0.3f, 2f).setOnUpdate( ( float val )=>{
  31. pushTrackAhead = val;
  32. });
  33. }
  34. void Update () {
  35. float zLastDist = (trackPts[ trackPts.Count - 1].z - transform.position.z);
  36. if(zLastDist < 200f){ // if the last node is too close we'll add in a new point and refresh the spline
  37. addRandomTrackPoint();
  38. refreshSpline();
  39. }
  40. // Update avatar's position on correct track
  41. track.place( car.transform, carIter );
  42. carIter += carAdd * Time.deltaTime;
  43. // we'll place the trail renders always a bit in front of the car
  44. track.place( trackTrailRenderers.transform, carIter + pushTrackAhead );
  45. // Switch tracks on keyboard input
  46. float turn = Input.GetAxis("Horizontal");
  47. if(Input.anyKeyDown){
  48. if(turn<0f && trackIter>0){
  49. trackIter--;
  50. playSwish();
  51. }else if(turn>0f && trackIter < 2){ // We have three track "rails" so stopping it from going above 3
  52. trackIter++;
  53. playSwish();
  54. }
  55. // Move the internal local x of the car to simulate changing tracks
  56. LeanTween.moveLocalX(carInternal, (trackIter-1)*6f, 0.3f).setEase(LeanTweenType.easeOutBack);
  57. }
  58. }
  59. // Simple object queuing system
  60. GameObject objectQueue( GameObject[] arr, ref int lastIter ){
  61. lastIter = lastIter>=arr.Length-1 ? 0 : lastIter+1;
  62. // Reset scale and rotation for a new animation
  63. arr[ lastIter ].transform.localScale = Vector3.one;
  64. arr[ lastIter ].transform.rotation = Quaternion.identity;
  65. return arr[ lastIter ];
  66. }
  67. void addRandomTrackPoint(){
  68. float randX = Mathf.PerlinNoise(0f, randomIter);
  69. randomIter += randomIterWidth;
  70. Vector3 randomInFrontPosition = new Vector3( (randX-0.5f)*20f, 0f, zIter*40f);
  71. // placing the box is just to visualize how the paths get created
  72. GameObject box = objectQueue( cubes, ref cubesIter );
  73. box.transform.position = randomInFrontPosition;
  74. // Line the roads with trees
  75. GameObject tree = objectQueue( trees, ref treesIter );
  76. float treeX = zIter%2==0 ? -15f : 15f;
  77. tree.transform.position = new Vector3( randomInFrontPosition.x + treeX, 0f, zIter*40f);
  78. // Animate in new tree (just for fun)
  79. LeanTween.rotateAround( tree, Vector3.forward, 0f, 1f).setFrom( zIter%2==0 ? 180f : -180f).setEase(LeanTweenType.easeOutBack);
  80. trackPts.Add( randomInFrontPosition ); // Add a future spline node
  81. if(trackPts.Count > trackMaxItems)
  82. trackPts.RemoveAt(0); // Remove the trailing spline node
  83. zIter++;
  84. }
  85. void refreshSpline(){
  86. track = new LTSpline( trackPts.ToArray() );
  87. carIter = track.ratioAtPoint( car.transform.position ); // we created a new spline so we need to update the cars iteration point on this new spline
  88. // Debug.Log("distance:"+track.distance+" carIter:"+carIter);
  89. carAdd = 40f / track.distance; // we want to make sure the speed is based on the distance of the spline for a more constant speed
  90. }
  91. // Make your own LeanAudio sounds at http://leanaudioplay.dentedpixel.com
  92. void playSwish(){
  93. AnimationCurve volumeCurve = new AnimationCurve( new Keyframe(0f, 0.005464481f, 1.83897f, 0f), new Keyframe(0.1114856f, 2.281785f, 0f, 0f), new Keyframe(0.2482903f, 2.271654f, 0f, 0f), new Keyframe(0.3f, 0.01670286f, 0f, 0f));
  94. AnimationCurve frequencyCurve = new AnimationCurve( new Keyframe(0f, 0.00136725f, 0f, 0f), new Keyframe(0.1482391f, 0.005405405f, 0f, 0f), new Keyframe(0.2650336f, 0.002480127f, 0f, 0f));
  95. AudioClip audioClip = LeanAudio.createAudio(volumeCurve, frequencyCurve, LeanAudio.options().setVibrato( new Vector3[]{ new Vector3(0.2f,0.5f,0f)} ).setWaveNoise().setWaveNoiseScale(1000));
  96. LeanAudio.play( audioClip ); //a:fvb:8,,.00136725,,,.1482391,.005405405,,,.2650336,.002480127,,,8~8,,.005464481,1.83897,,.1114856,2.281785,,,.2482903,2.271654,,,.3,.01670286,,,8~.2,.5,,~~0~~3,1000,1
  97. }
  98. }