SplineWalker.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="SplineWalker.cs" company="Exit Games GmbH">
  3. // Part of: Photon Unity Networking Demos
  4. // </copyright>
  5. // <summary>
  6. // Original: http://catlikecoding.com/unity/tutorials/curves-and-splines/
  7. // Used in SlotRacer Demo
  8. // </summary>
  9. // <author>developer@exitgames.com</author>
  10. // --------------------------------------------------------------------------------------------------------------------
  11. using UnityEngine;
  12. namespace Photon.Pun.Demo.SlotRacer.Utils
  13. {
  14. [ExecuteInEditMode]
  15. public class SplineWalker : MonoBehaviour {
  16. public BezierSpline spline;
  17. public float Speed = 0f;
  18. public bool lookForward;
  19. public bool reverse;
  20. private float progress;
  21. public float currentDistance =0f;
  22. public float currentClampedDistance;
  23. public void SetPositionOnSpline(float position)
  24. {
  25. currentDistance = position;
  26. ExecutePositioning ();
  27. }
  28. void Update()
  29. {
  30. // update the distance used.
  31. currentDistance += Speed * Time.deltaTime;
  32. ExecutePositioning ();
  33. }
  34. public void ExecutePositioning()
  35. {
  36. if(spline==null)
  37. {
  38. return;
  39. }
  40. // move the transform to the new point
  41. transform.position = spline.GetPositionAtDistance(currentDistance,this.reverse);
  42. // update the distance used.
  43. currentDistance += Speed * Time.deltaTime;
  44. if (lookForward) {
  45. transform.LookAt(spline.GetPositionAtDistance(currentDistance+1,this.reverse));
  46. }
  47. }
  48. }
  49. }