I can’t help you with .Lerp () because it’s a complex thing to understand and I didn’t see it very well.
But I can help with what you are trying to do. First, are you moving the GameObject through a Rigidbody? Second, is the GameObject a projectile? Correct me if I’m wrong.
If this is a projectile. Then you can apply the physics projectile motion formula.
The formula says this:
Note: T is time.
X is the distance between the two points horizontally.
Y it’s the height.
A is the acceleration of gravity, in this case it will be -9.81. (If you look at Unity Physics, you will see this value. For more information here).
Pow means “elevated to the power of”.
X = V0x * Cos(angle) * T
Y = V0y * Sin(angle) - 1/2 * A * Pow(T, 2)
After seeing this, maybe you noticed that you have X, Y and in T you can give a value. So, in this case, you need V0x and V0y for the initial momentum. But first you need to know the angle generated by the two points. Then we need to apply the trigonometric functions to get the angles.
If you see vectors you can do this:
float Acos(float c) => Mathf.Acos(c) * 180 / Mathf.PI; // function to get the arc-cosine in degrees
float Asin(float s) => Mathf.Asin(s) * 180 / Mathf.PI; // function to get the arc-sine in degrees
Vector2 distSide = target - origin; // Subtract target by origin to get side distance
float h = distSide.magnitude; // Get the magnitude of distSide. This will be the hypotenuse.
// Angle by the Cos method
float cos = dist.x / h // adjacent over the hypotenuse
float resultCos = cos >= 0 ? Mathf.Round(Acos(cos)) : Mathf.Round(Acos(-cos)); // Make a check because it will always return positive grades
// ---
// Angle by Sin method
float sin = dist.y / h // opposite over the hypotenuse
float resultSin = Mathf.Round(Asin(sin));
// ---
Now we need two values that we don’t know. And these are V0x and V0y. Then we can make an equation and the result will be this:
V0x = X / Cos(angle) * T
V0y = Y / Sin(angle) * T + 1/2 * A * T
So now we need to know these values. And if we pass this to the script, it will be something like this:
// Formulas
float Vx(float x, float t) => x / (Mathf.Cos(resultCos / 180 * Mathf.PI) * t);
// I do a Mathf.Abs to the Mathf.Sin because sometimes this returns a negative value.
// And also apply .Abs to Physics2D.gravity.y because this is negative
float Vy(float y, float t) => y / (Mathf.Abs(Mathf.Sin(resultSin / 180 * Mathf.PI)) * t) + .5f * Mathf.Abs(Physics2D.gravity.y) * t;
// ----
float hY = target.y - origin.y; // Get the hight
float dX = target.x - origin.x; // Get the distance on the X axis
Vector2 v0 = new Vector2(dX, 0).normalized; // Assign a new normailized Vector2 in v0.
v0 *= Vx(Mathf.Abs(dX)); // Multiply the Vx by the v0. Use Mathf.Abs to convert negative values to positive.
v0.y = Vy(hY); // Assign in Y the Vy
// Assign v0 to rigidbody
rb2D.velocity = v0;
You can even remove t from the formulas. Since this value affects how the parabola is formed at the time of the flight. But if you want to generate a normal parable. So just having the angles is enough, because they apply a normal time without needing t.
I hope this helps.