# Projectile Trajectory

I have been working on creating a simple bow/arrow type gameobject.

after searching the forums and google, I have started using the following

``````#pragma strict

function Start () {

}

var target: Transform;
var arrowPrefab: Transform;
var arrowSP: Transform;

function Update(){
if (Input.GetMouseButtonDown(0)){
var arrow: Transform = Instantiate(arrowPrefab,arrowSP.transform.position, arrowSP.transform.rotation);
arrow.rigidbody.velocity = BallisticVel(target, 40); // pass the angle and the target transform
}
}

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
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;
}
``````

script:

I also have this on my arrow Prefab to keep it facing the right way and handle the collision.

``````#pragma strict

var hit: boolean;

function Start () {

}

function FixedUpdate(){
if(!hit){
if(rigidbody.velocity != Vector3.zero)
rigidbody.rotation = Quaternion.LookRotation(rigidbody.velocity);
}
}

yield WaitForSeconds (10);
Destroy (gameObject);
}

function OnCollisionEnter (col : Collision){

if(!hit){
if(col.gameObject.tag == "ground"){
transform.Translate(0,0,Random.Range(0.05, .5));
Destroy (rigidbody);
hit = true;
}
}
}
``````

My problem is that it seems that the arrorw always lands just past the target object. The target’s x coord will be 20 and the arrow’s point of impact x coord is 24 missing the object. Seems to always be off by 4 from what I’ve seen no mater where I move the target on the x plane.

Can anyone see whats wrong?