I am using the following to calculate velocity: https://gamedev.stackexchange.com/a/114547/39518
private void CalculateVelocity()
{
Vector3 toTarget = Target.position - StartingPoint.position;
// Set up the terms we need to solve the quadratic equations.
float gSquared = Physics.gravity.sqrMagnitude;
float b = Speed * Speed + Vector3.Dot(toTarget, Physics.gravity);
float discriminant = b * b - gSquared * toTarget.sqrMagnitude;
// Check whether the target is reachable at max speed or less.
if (discriminant < 0)
{
// Target is too far away to hit at this speed.
// Abort, or fire at max speed in its general direction?
}
float discRoot = Mathf.Sqrt(discriminant);
// Highest shot with the given max speed:
float T = Mathf.Sqrt((b + discRoot) * 2f / gSquared);
// Convert from time-to-hit to a launch velocity:
Velocity = toTarget / T - Physics.gravity * T / 2f;
}
I'd now like to take the calculated velocity and move the projectile using the following: https://gamedev.stackexchange.com/a/119636/39518
I came up with the following: "T" & "Velocity" are variables calculated in the first method.
private IEnumerator MoveProjectileToTarget()
{
while(Projectile.position != Target.position)
{
// Standard ballistic motion:
Vector3 point = StartingPoint.position
+ T * Velocity
+ 0.5f * T * T * (Vector3)(Physics.gravity);
Projectile.position = point;
yield return null;
}
}
The current problem I have is the projectile is warping to it's target. I believe the issue lies in the while loop I created. Any help would be greatly appreciated. Thanks!
p(t) = p_0 + v_0 * t + a/2 * t*t
). Presumably you tried accumulating the time since launch and plugging it into that formula? Help us understand where in this process you got stuck, or what kind of help you need. \$\endgroup\$predictionSeconds
or_points
. The other answer is about making a list of points to draw an arc. That's not what you're doing, so you don't need that. The only part of this answer you need is the line that beginsVector3 point = ...
- that is your formula for where the projectile should bet
seconds after launch. Put that in your update loop or coroutine or whatever you're using to set the projectile's position each frame. \$\endgroup\$