Move an object in X seconds with varying height

I have an object that is following a trajectory path using transform.Translate to move in X-Z axis and with transform.position to calculate the height based on the center of the path.

Problem is, I can’t make it travel in X seconds and AFAIK the only to make it happen is using Vector3.Lerp which guarantees a time-based travel. And with Lerp, it’s impossible to simulate a throw.

I’m not using Rigidbody. Just some math and vector calculation.

111333-a.png

Alright, here is how I did it: First, ignore the height and move the object like it’s moving horizontally. At each loop iteration, set it’s height, without spoiling X and Z coordinates (plane).

I actually discovered this while loop and time iteration from this answer. Huge thanks to @Lakeffect

            // COROUTINE OR UPDATE.
            _elapsedTime = 0;

            var lerpStart = new Vector3(3, 0, 5);
            var lerpEnd = new Vector3(10, 0, 15);
            var maxY = Mathf.Abs(Mathf.Pow(Vector3.Distance(transform.position, halfway), 2));

            while (_elapsedTime < EXPECTED_TIME)
            {
               // SEE ME ROLLING
               Debug.DrawLine(lerpStart, lerpEnd, Color.red);

               // DISTANCE TO MIDDLE.
               var subs = Vector3.Distance(transform.position, halfway);

               // MOVE HORIZONTALLY IGNORING HEIGHT.
               transform.position = Vector3.Lerp(lerpStart, lerpEnd, _elapsedTime / EXPECTED_TIME);
                
               // SET OBJECT HEIGHT AFTER MOVING
               transform.position = new Vector3(transform.position.x, (maxY - Mathf.Pow(subs, 2)) * VerticalScale, transform.position.z);

              // RECORD TIME
               _elapsedTime += Time.deltaTime;

              // I was using a coroutine to move the object few times, this is why I have this line.
               yield return null;
            }