C# Shooting a cannonball (ballistics)

Hey guys, I am working on a TowerDefense/ MOBA game . Currently I am dealing with an issue concerning my CannonTower.
Please take a look at the screenshot I have uploaded (the link). As you can see the tower is supposed to shoot at the little mummy fellow.
I have tried several scripts and ways to make this happen.
First I tried all transform.Translate commands I knew, much like the AddForce commands the result was not really what I had in mind.
I then stumbled onto a thread in this forum concerning the issue written in (of course -.-) unity script. I tried to translate this script to c# - I had to leave out a part to make it work. The cannonball is moving the way I want it to fly, but as far as I understand the script was never really intended to “hit” the target. I tried to understand all the Mathf. functions - but well you know… english… math… + beginner in coding = bad.
It would be great if I could find a way to make the cannonball move exactly the way it is in this script ,BUT increase the entire flight accordingly.
If the enemy is very close - the ball would have to fly only a little faster,however if the enemy is further away the ball would have to fly a lot faster.

This is the original unity script code :

function BallisticVel(target: Transform, angle: float): 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
 
        var a = angle * Mathf.Deg2Rad;  // convert angle to radians
 
        dir.y = dist * Mathf.Tan(a);  // set dir to the elevation angle
 
        dist += h / Mathf.Tan(a);  // correct for small height differences
 
        // calculate the velocity magnitude
        var vel = Mathf.Sqrt(dist * Physics.gravity.magnitude / Mathf.Sin(2 * a));
 
        return vel * dir.normalized;

}

       var myTarget: Transform;  // drag the target here
var cannonball: GameObject;  // drag the cannonball prefab here
var shootAngle: float = 30;  // elevation angle

function Update(){
 if (Input.GetKeyDown("b")){  // press b to shoot
 var ball: GameObject = Instantiate(cannonball, transform.position, Quaternion.identity);
 ball.rigidbody.velocity = BallisticVel(myTarget, shootAngle);
 Destroy(ball, 10);
 }
} 

I translated the code but had to leave out the “angle” part - because I couldn’t make it work.

This is what I got:

 GameObject ball = Instantiate(cannonballprefab,shootCannonPos,Quaternion.identity)as GameObject;
 ball.rigidbody.velocity = BallisticVel(enemyObject);

 public Vector3 BallisticVel(Transform target){
 Vector3 dir = target.position - mySelf.transform.position;
 float h = dir.y;
 dir.y =0;
 float dist = dir.magnitude;
 dir.y = dist;
 dist += h;
 float vel = Mathf.Sqrt(dist*Physics.gravity.magnitude);
 return vel*dir.normalized;
 }

Here is the LINK to the scene:

in case that link won’t work :

All this colliding,exploding,health stuff shouldn’t be an issue…just that fly with a curve - and actually hit the damn target is :wink:

Thanks guys!

This script is from an answer of mine, and there’s a C# version in Use a vector3 position to calculate where a bomb falls - Questions & Answers - Unity Discussions

The cannonball must be a simple rigidbody. The BallisticVel function was derived from a basic formula intended to hit a target at the same height. I added some extra calculation to compensate to some extent for height differences - this works fine when the horizontal distance is bigger than the vertical distance (3 times or more).