How to make enemy Cannonball fall on moving target position.

How do I make enemy cannon ball fall on target position?

My target (PLAYER) is moving and I would like the enemy turret (which catapults cannonball in arc) to be always able to hit target position - where it was recently standing when the cannon ball was catapulted.

So that if target would stand still the cannonball would actually land on target.

So far my cannon ball always lands in same distance from turret.

The easiest way to do that is to shoot always at a 45 degrees elevation, and adjust the velocity to reach the target distance (45 degrees gives the greatest range - read about it in the Temple of Wisdom, aka Wikipedia). If the target and the turret are at the same height, the initial cannon ball velocity is given by Mathf.Sqrt(distance * gravity).
The function BallisticVel in the script below returns the velocity necessary to hit the object defined by myTarget:

```var myTarget: Transform;
var cannonball: GameObject;

function BallisticVel(target: Transform): Vector3 {

var dir = target.position - transform.position; // get target direction
var h = dir.y;  // get height difference
dir.y = 0;  // retain only the horizontal direction
var dist = dir.magnitude ;  // get horizontal distance
dir.y = dist;  // set elevation to 45 degrees
dist += h;  // correct for different heights
var vel = Mathf.Sqrt(dist * Physics.gravity.magnitude);
return vel * dir.normalized;  // returns Vector3 velocity
}

function Update(){

if (Input.GetKeyDown("b")){
var ball: GameObject = Instantiate(cannonball, transform.position, transform.rotation);
ball.rigidbody.velocity = BallisticVel(myTarget);
Destroy(ball, 10);
}
}

```

To test this script, assign it to an empty object, drag the target to myTarget and your cannon ball prefab to cannonball, then press the key b to shot.

NOTE: Set the object position to a height that ensures the cannon ball will not touch any collider when instantiated, or this will alter the cannon ball movement and it will never reach the target. If you want to place the game object in your turret, set Is Trigger in the turret’s collider to avoid this interference.

EDITED: I modified the script a little to compensate for small differences in height between the turret and the target. The idea is: the cannon ball reaches the destination point at the same angle it was launched; since we are shooting at 45 degrees, any difference in height will produce a similar difference in the distance - at least near to the calculated impact point. In practical terms, if the target is 5 units above the turret, I can shoot to a point 5 units farther, and the cannon ball trajectory will pass through the actual player position. It’s a valid approximation for height differences about up to 20% of the horizontal distance, but the error may become excessive outside this range. There’s a complete equation in the reference I provided, but it seems impossible to solve with linear methods, so this approximation may be the best alternative.