using System.Collections; using System.Collections.Generic; using UnityEngine; namespace EasyTalk.Demo { /// /// Used to move an object along a curve over time. /// public class CurvedPath : MonoBehaviour { /// /// The object to move along the curve. /// [SerializeField] private GameObject movedObject; /// /// The start point of the curve. /// [SerializeField] private Transform start; /// /// The mid-point of the curve. /// [SerializeField] private Transform control; /// /// The end point of the curve. /// [SerializeField] private Transform end; /// /// The current position (between 0.0 and 1.0) along the path. /// [SerializeField] private float position = 0.0f; /// /// Whether the curved path follower is being played. /// [SerializeField] private bool isPlaying = false; /// /// Returns a Vector3 position which lies on the curve going from start to control to end based on the value provided. /// /// The start point of the curve. /// The control/mid-point of the curve. /// The end point of the curve. /// How far along the curve a position is to be retrieved for (from 0.0 to 1.0). /// public static Vector3 GetPoint(Vector3 start, Vector3 control, Vector3 end, float amount) { return Vector3.Lerp(Vector3.Lerp(start, control, amount), Vector3.Lerp(control, end, amount), amount); } void Update() { if (isPlaying) { Vector3 oldPos = movedObject.transform.position; movedObject.transform.position = GetPoint(start.position, control.position, end.position, position); Vector3 newPos = movedObject.transform.position; movedObject.transform.LookAt((newPos - oldPos) + newPos, movedObject.transform.up); } } } }