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.

68 lines
3.0 KiB

7 years ago
  1. using UnityEngine;
  2. using System.Collections;
  3. using DentedPixel;
  4. // This project demonstrates how you can use the spline behaviour for a multi-track game (like an endless runner style)
  5. public class PathSplineTrackCS : MonoBehaviour {
  6. public GameObject car;
  7. public GameObject carInternal;
  8. public GameObject trackTrailRenderers;
  9. public Transform[] trackOnePoints;
  10. private LTSpline track;
  11. private int trackIter = 1;
  12. private float trackPosition; // ratio 0,1 of the avatars position on the track
  13. void Start () {
  14. // Make the track from the provided transforms
  15. track = new LTSpline( new Vector3[] {trackOnePoints[0].position, trackOnePoints[1].position, trackOnePoints[2].position, trackOnePoints[3].position, trackOnePoints[4].position, trackOnePoints[5].position, trackOnePoints[6].position} );
  16. // Optional technique to show the trails in game
  17. LeanTween.moveSpline( trackTrailRenderers, track, 2f ).setOrientToPath(true).setRepeat(-1);
  18. }
  19. void Update () {
  20. // Switch tracks on keyboard input
  21. float turn = Input.GetAxis("Horizontal");
  22. if(Input.anyKeyDown){
  23. if(turn<0f && trackIter>0){
  24. trackIter--;
  25. playSwish();
  26. }else if(turn>0f && trackIter < 2){ // We have three track "rails" so stopping it from going above 3
  27. trackIter++;
  28. playSwish();
  29. }
  30. // Move the internal local x of the car to simulate changing tracks
  31. LeanTween.moveLocalX(carInternal, (trackIter-1)*6f, 0.3f).setEase(LeanTweenType.easeOutBack);
  32. }
  33. // Update avatar's position on correct track
  34. track.place( car.transform, trackPosition );
  35. trackPosition += Time.deltaTime * 0.03f;// * Input.GetAxis("Vertical"); // Uncomment to have the forward and backwards controlled by the directional arrows
  36. if (trackPosition < 0f) // We need to keep the ratio between 0-1 so after one we will loop back to the beginning of the track
  37. trackPosition = 1f;
  38. else if(trackPosition>1f)
  39. trackPosition = 0f;
  40. }
  41. // Use this for visualizing what the track looks like in the editor (for a full suite of spline tools check out the LeanTween Editor)
  42. void OnDrawGizmos(){
  43. LTSpline.drawGizmo( trackOnePoints, Color.red);
  44. }
  45. // Make your own LeanAudio sounds at http://leanaudioplay.dentedpixel.com
  46. void playSwish(){
  47. 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));
  48. 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));
  49. AudioClip audioClip = LeanAudio.createAudio(volumeCurve, frequencyCurve, LeanAudio.options().setVibrato( new Vector3[]{ new Vector3(0.2f,0.5f,0f)} ).setWaveNoise().setWaveNoiseScale(1000));
  50. 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
  51. }
  52. }