# Projectile trajectory

Hi,

Im new to unity and im trying to make a game quite simple. i got a tower in the middle of the screen on which there a cannon. i need to calculate the trajectory of a cannon ball which i want to land where i click on the field (like a RTS view or D2 view).

i tried :

``````Vector3 Calculate(Vector3 p1, Vector3 p2)
{
float initialSpeed = 10.0f;
float range = (p2 - p1).magnitude;
float gravity = 9.8f;

float angle = 0.5f * (Mathf.Asin(gravity * range / (initialSpeed * initialSpeed)) ;

Vector3 horizontalDirection = (p2 - p1).normalized;

Vector3 verticalDirection = Vector3.up;

Vector3 initialVelocityHorizontal = initialSpeed * Mathf.Cos(angle) * horizontalDirection;

Vector3 initialVelocityVertical = initialSpeed * Mathf.Sin(angle) * verticalDirection;

Vector3 initialVelocity = initialVelocityHorizontal + initialVelocityVertical;

return initialVelocity;
}

void FireBullet()
{
GameObject Bullet = Instantiate(CannonBall, transform.position, transform.rotation) as GameObject;

RaycastHit hit = new RaycastHit();

Ray ray = Camera.allCameras[0].ScreenPointToRay(Input.mousePosition);

if (Physics.Raycast(ray, out hit, 10000))
{

float Vx, Vz;

Vx = (hit.point.x - transform.position.x) / 0.11111f;
Vz = (hit.point.z - transform.position.z) * 9.0003f;
Debug.Log(Vx + " :: " + Vz);
Debug.Log(hit.point.x.ToString() + " :: " + hit.point.z.ToString());

Bullet.rigidbody.velocity = Calculate(transform.position, hit.point); ;

}
}

``````

but i'm getting angle always = to NaN... so nothing work at all

Hope you can help

i think ur question same like my question..