SplinePosition.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="SplinePosition.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 SplinePosition : MonoBehaviour {
  16. public BezierSpline Spline;
  17. public bool reverse;
  18. public bool lookForward;
  19. public float currentDistance = 0f;
  20. public float currentClampedDistance;
  21. float LastDistance;
  22. public void SetPositionOnSpline(float position)
  23. {
  24. this.currentDistance = position;
  25. ExecutePositioning ();
  26. }
  27. void Update()
  28. {
  29. ExecutePositioning ();
  30. }
  31. void ExecutePositioning()
  32. {
  33. if(this.Spline==null || this.LastDistance == this.currentDistance )
  34. {
  35. return;
  36. }
  37. LastDistance = this.currentDistance;
  38. // move the transform to the new point
  39. this.transform.position = this.Spline.GetPositionAtDistance(currentDistance,this.reverse);
  40. if (this.lookForward) {
  41. this.transform.LookAt(this.Spline.GetPositionAtDistance(currentDistance+1,this.reverse));
  42. }
  43. }
  44. }
  45. }