Trying to fire an Arrow

HI, I am trying to fire a realistic arrow, but when i fire it, it comes out sideways and does not behave properly in the air, it just spins.

void Update ()
{		
	if (Input.GetMouseButtonDown(0))
	{
		//Launch arrow.
		Rigidbody clone;				
		
		clone = Instantiate(arrow, transform.position, transform.rotation) as Rigidbody;			
		clone.velocity = transform.TransformDirection(Vector3.forward * 100);
		
		Quaternion rot = transform.rotation;
		rot.SetLookRotation (rigidbody.velocity);			
		transform.rotation = rot;			
	}
}

}

1 Answer

1

With a rotation of (0,0,0), the head of your arrow must point at positive ‘Z’. If it does not, either fix it in your modeling program or you can make the arrow a child of an empty game object and rotate it with respect to the empty game object.

I’m not sure what you are trying to do with lines 11-13. Take them out. Your arrow should fire in the forward direction of the spawn point (which I assume this script is attached to).

As an alternate for firing, try this instead:

clone.rigidbody.AddForce(clone.transform.forward * 1000.0f);

Your firing code should work, but this is another way that might be better in the long run.

Add a script with this attached to your arrow prefab: void FixedUpdate () { transform.LookAt (transform.position + rigidbody.velocity); }

Works like a charm! TY!

Close out the question by clicking on the checkmark next to the answer.