BezierCurve.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="BezierCurve.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. public class BezierCurve : MonoBehaviour
  15. {
  16. public Vector3[] points;
  17. public Vector3 GetPoint (float t)
  18. {
  19. return transform.TransformPoint(Bezier.GetPoint(points[0], points[1], points[2], points[3], t));
  20. }
  21. public Vector3 GetVelocity (float t)
  22. {
  23. return transform.TransformPoint(Bezier.GetFirstDerivative(points[0], points[1], points[2], points[3], t)) - transform.position;
  24. }
  25. public Vector3 GetDirection (float t)
  26. {
  27. return GetVelocity(t).normalized;
  28. }
  29. public void Reset ()
  30. {
  31. points = new Vector3[] {
  32. new Vector3(1f, 0f, 0f),
  33. new Vector3(2f, 0f, 0f),
  34. new Vector3(3f, 0f, 0f),
  35. new Vector3(4f, 0f, 0f)
  36. };
  37. }
  38. }
  39. }