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.

93 lines
3.7 KiB

7 years ago
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using DentedPixel;
  5. // This class is to test the upper limits of a complex spline
  6. public class PathSplinePerformanceCS : MonoBehaviour {
  7. public GameObject trackTrailRenderers;
  8. public GameObject car;
  9. public GameObject carInternal;
  10. public float circleLength = 10f;
  11. public float randomRange = 1f;
  12. public int trackNodes = 30;
  13. public float carSpeed = 30f;
  14. public float tracerSpeed = 2f;
  15. private LTSpline track;
  16. private int trackIter = 1;
  17. private float carAdd;
  18. private float trackPosition; // ratio 0,1 of the avatars position on the track
  19. void Start () {
  20. Application.targetFrameRate = 240;
  21. // Make the track from the provided transforms
  22. List<Vector3> randList = new List<Vector3>();
  23. float degree = 0f;
  24. int nodeLength = trackNodes + 1;// We need to add some extra because the first and last nodes just act as *guides* to the first and last curvature
  25. for(int i = 0; i < nodeLength; i++){
  26. float x = Mathf.Cos( degree * Mathf.Deg2Rad ) * circleLength + Random.Range(0f, randomRange);
  27. float z = Mathf.Sin( degree * Mathf.Deg2Rad ) * circleLength + Random.Range(0f, randomRange);
  28. randList.Add( new Vector3(x,1f,z ) );
  29. degree += 360f/(float)trackNodes;
  30. }
  31. randList[0] = randList[ randList.Count-1 ]; // set the zero-ith one as the last position so it will flow smoothly into the first curve
  32. randList.Add( randList[1] ); // Add the first and second one in, so the circle connects to itself
  33. randList.Add( randList[2] );
  34. track = new LTSpline( randList.ToArray() );
  35. carAdd = carSpeed / track.distance;
  36. tracerSpeed = track.distance / (carSpeed*1.2f);
  37. // Optional technique to show the trails in game
  38. LeanTween.moveSpline( trackTrailRenderers, track, tracerSpeed ).setOrientToPath(true).setRepeat(-1);
  39. }
  40. void Update () {
  41. // Switch tracks on keyboard input
  42. float turn = Input.GetAxis("Horizontal");
  43. if(Input.anyKeyDown){
  44. if(turn<0f && trackIter>0){
  45. trackIter--;
  46. playSwish();
  47. }else if(turn>0f && trackIter < 2){ // We have three track "rails" so stopping it from going above 3
  48. trackIter++;
  49. playSwish();
  50. }
  51. // Move the internal local x of the car to simulate changing tracks
  52. LeanTween.moveLocalX(carInternal, (trackIter-1)*6f, 0.3f).setEase(LeanTweenType.easeOutBack);
  53. }
  54. // Update avatar's position on correct track
  55. track.place( car.transform, trackPosition );
  56. trackPosition += Time.deltaTime * carAdd;
  57. if(trackPosition>1f)
  58. trackPosition = 0f; // We need to keep the ratio between 0-1 so after one we will loop back to the beginning of the track
  59. }
  60. // Use this for visualizing what the track looks like in the editor (for a full suite of spline tools check out the LeanTween Editor)
  61. void OnDrawGizmos(){
  62. if(track!=null)
  63. track.drawGizmo( Color.red );
  64. }
  65. // Make your own LeanAudio sounds at http://leanaudioplay.dentedpixel.com
  66. void playSwish(){
  67. 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));
  68. 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));
  69. AudioClip audioClip = LeanAudio.createAudio(volumeCurve, frequencyCurve, LeanAudio.options().setVibrato( new Vector3[]{ new Vector3(0.2f,0.5f,0f)} ).setWaveNoise().setWaveNoiseScale(1000));
  70. 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
  71. }
  72. }