Calculate parabolic shot...

Hi, Im having some troubles, not actually coding (maybe) but perhaps math problems, Here’s the thing.
I have some kind of AI who makes shoots at you in a parabolic trayectory, now, I dont know how to succesfully calculate the force neded to apply into te proyectile in order to reach the target, I only know the distance betwen the shoter and the target with
Vector3 target;
Vector3 distanceFromTarget = target - transform.position;
im giving some “randomish” angle to shot, based on the obstacles the AI have on his way but giving some angles more than 0 and below than 90, i cannot calculate the initial velocity.
Im using Unity Physix for the bullets, when I shoot them it works well, I can manually calculate my trayectory and do some “Test and error” shots but, the AI must be capable of calculate the same parameters with a formula.
Here’s the code i have right now:

    void GetElevationDirection()
    {
        targetPosition = currenTarget.transform.position;
        vectorResultante = targetPosition - cannonPlace;
        estimatedElevation = Random.Range(30, 50);
        float finalAngle = estimatedElevation * 2;
        finalAngle = Mathf.Sin(finalAngle * Mathf.Deg2Rad);  
        float gx = 9.81F * vectorResultante.magnitude;
        estimatedSpeed = Mathf.Sqrt(gx / finalAngle);
        //estimatedSpeed *= 1.25F;
        estimatedElevation *= -1;
    }

I need to multiply at the end the “estimatedElevation” variable because when I rotate the 3D model of the cannon, it have inverted X axis and in that way the cannon points up and not down, but the estimated speed, is to low, even with the 25% increment, and somethimes I get an error on the Matfh.Sqrt because the “gx / finalAngle” division gets negative. I hope that someone can help me…

I dont know how to succesfully calculate the force neded to apply into te proyectile in order to reach the target, I only know the distance betwen the shoter and the target… i cannot calculate the initial velocity.

It sounds like you should look up the math of projectile motion. The relevant equations should be available in physics textbooks on paper or the Web. You’ll want to look at the situation like this, I think:

If you have a cannon firing with initial velocity described as Vx and Vy (horizontal and vertical axes), then the distance traveled horizontally at time T is X = (Vx) * T. The current height Y = (Vy)*T - (.5)gT^2, where “g” is the acceleration of gravity (usually estimated as 9.81 m/s^2 near Earth’s surface, but you’ll have to translate to Unity units) and “T^2” is T squared.

To find the initial velocity required to hit some point XY, not considering any obstacles in the way, you need to solve this “system of equations” for Vx and Vy. I would define a cannon’s power not as Vx and Vy directly, but Vo (original velocity) and an angle Theta up from the ground. (0-180 degrees or 0 to pi radians.) Then, you can find Vx and Vy as “Vo cos Theta” and “Vo sin Theta”. Now for any given velocity, you can just solve for Theta, or vice versa. One option then is to check several possible velocities and see if any Theta value will hit the target at that velocity.

If you’re looking to calculate force rather than initial velocity, the math gets a bit more complex, but figure that F = ma, and you’re applying this force over some time period (realistically, the time that the cannonball is flying out of the cannon) to get enough acceleration over time to get the velocity you want.

As for angles going negative, can you take the absolute value of the result?