LerpOld.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using UnityEngine;
  2. public class LerpOld : MonoBehaviour
  3. {
  4. private Transform startPoint, endPoint;
  5. [SerializeField]
  6. private Transform[] waypoints;
  7. public float speed = 1.0f;
  8. private int currentStartPoint;
  9. private float startTime;
  10. private float journeyLength;
  11. private void Start()
  12. {
  13. currentStartPoint = 0;
  14. transform.position = waypoints[currentStartPoint].position;
  15. SetPoints();
  16. }
  17. void SetPoints()
  18. {
  19. startPoint = waypoints[currentStartPoint];
  20. endPoint = waypoints[currentStartPoint + 1];
  21. startTime = Time.time;
  22. journeyLength = Vector3.Distance(startPoint.position, endPoint.position);
  23. }
  24. // Update is called once per frame
  25. void Update()
  26. {
  27. float distCovered = (Time.time - startTime) * speed;
  28. float fracJourney = distCovered / journeyLength;
  29. transform.position = Vector3.Lerp(startPoint.position, endPoint.position, fracJourney);
  30. //transform.rotation = Quaternion.Lerp(startPoint.rotation, endPoint.rotation, fracJourney);
  31. if (fracJourney >= 1f && currentStartPoint + 2 < waypoints.Length)
  32. {
  33. currentStartPoint++;
  34. SetPoints();
  35. }
  36. }
  37. }