123456789101112131415161718192021222324252627282930313233343536373839404142 |
- using UnityEngine;
- public class Lerp : MonoBehaviour
- {
- private Transform startPoint, endPoint;
- [SerializeField]
- private Transform[] waypoints;
- public float speed = 1.0f;
- private int currentStartPoint;
- private float startTime;
- private float journeyLength;
- private void Start()
- {
- currentStartPoint = 0;
- transform.position = waypoints[currentStartPoint].position;
- SetPoints();
- }
- void SetPoints()
- {
- startPoint = waypoints[currentStartPoint];
- endPoint = waypoints[currentStartPoint + 1];
- startTime = Time.time;
- journeyLength = Vector3.Distance(startPoint.position, endPoint.position);
- }
- // Update is called once per frame
- void Update()
- {
- float distCovered = (Time.time - startTime) * speed;
- float fracJourney = distCovered / journeyLength;
- transform.position = Vector3.Lerp(startPoint.position, endPoint.position, fracJourney);
- //transform.rotation = Quaternion.Lerp(startPoint.rotation, endPoint.rotation, fracJourney);
- if (fracJourney >= 1f && currentStartPoint + 2 < waypoints.Length)
- {
- currentStartPoint++;
- SetPoints();
- }
- }
- }
|