# question about RigidBody.AddForce -- applied to bullets

I have a sphere (a "ship") in the middle of the screen at (0,0,0). Bullets "fire" from the middle of the sphere, but they fire in the direction of the rotation of the sphere about the z-axis (imagine the viewer is staring down the z-axis).

So, I thought I'd just take the tangent of the angle of rotation and apply a force to the rigidbody of the bullet. The bullet seems to fire in the right direction, but for some reason it is being displaced before it fired, such that it does not fire from the center of the sphere ("ship"), but rather to the left of it slightly.

Here is how I'm calculating the force in the bullet script.

``````function Update () {
if (!isFired)
{
alpha = ship.rotation.z * Mathf.Deg2Rad;
transform.rigidbody.AddForce(Vector3(-Mathf.Tan(alpha), 1, 0) * bulletSpeed);
isFired = true;
}
}

``````

Keep in mind the original position of the ship is (0,0,0), and the original position of a bullet is (0,0,0). All I do is use the AddForce function to send the bullet of in a specific direction.

Any thoughts?

Ok, figured it out. Should have been using AddRelativeForce... duh.

``````if (!isFired)
{
alpha = transform.rotation.z * Mathf.Deg2Rad;
transform.rigidbody.AddRelativeForce(Vector3.up * bulletSpeed);
isFired = true;
}

``````

If the bullet and the ship have solid colliders then they will displace each other. It's not a bug.

Just instantiate the bullet outside the ship in the direction it's facing.

e.g.

``````var bullet : GameObject; // drag your bullet prefab here in the inspector
var shipRadius : float = 2; // how big the ship is

function Fire(){
Instantiate(bullet,transform.position + transform.TransformDirection(Vector3.forward)*shipRadius,transform.rotation);
}

``````