using UnityEngine; namespace HPJ.Examples { /// /// An Example Script to ping pong an object between 2 points /// public class ObstaclePingPong : MonoBehaviour { public Transform startPoint; public Transform endPoint; public float speed = 5.0f; [Range(0, 1)] public float startingPercent = 0; public bool ReverseDirection = false; private float pingPongTime = 0.0f; private void Start() { if (ReverseDirection) { startingPercent = 2 - startingPercent; } pingPongTime = startingPercent; } private void OnValidate() { transform.position = Vector3.Lerp(startPoint.position, endPoint.position, startingPercent); } void Update() { pingPongTime += Time.deltaTime * speed; float pingPongValue = Mathf.PingPong(pingPongTime, 1.0f); transform.position = Vector3.Lerp(startPoint.position, endPoint.position, pingPongValue); } } }