0
\$\begingroup\$

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!

\$\endgroup\$
4
  • \$\begingroup\$ Where are you having trouble moving the object along the path? The answer you've linked there shows a formula into which you can plug any value for the number of seconds since launch and get the position of the projectile (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\$
    – DMGregory
    Jan 15 at 22:27
  • \$\begingroup\$ Ack, you're right. I'll provide more details. Sorry about that! \$\endgroup\$ Jan 15 at 22:38
  • \$\begingroup\$ Don't use 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 begins Vector3 point = ... - that is your formula for where the projectile should be t 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\$
    – DMGregory
    Jan 16 at 1:05
  • \$\begingroup\$ Oh, got it! Thanks! I modified the method I was using and edited my post. \$\endgroup\$ Jan 16 at 2:27

1 Answer 1

0
\$\begingroup\$

You're currently calculating just the position at the END of the arc (time T is the total time it takes to finish the whole flight). Did you mean to gradually step a t value from 0 up to T?

private IEnumerator MoveProjectileToTarget()
{
    for (float t = 0; t < T; t += Time.deltaTime) {
        Projectile.position = StartingPoint.position
                            + t * Velocity
                            + 0.5f * t * t * Physics.gravity;

        yield return null;
    }        
}
\$\endgroup\$
1
  • \$\begingroup\$ I certainly did! Code works perfectly now! \$\endgroup\$ Jan 16 at 3:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .