Shoot in a straight line

Hi,

I am trying to make a car shooter, where you shoot enemy cars. I have this script for a bullet:

    //Attach to empty object, in front of barrel
var projectile : Rigidbody;
var speed = 20;
var RotateX = 0;
var RotateY = 0;
var RotateZ = 0;
function Update() 
{
    if( Input.GetButton( "Fire1" ) ) {

        var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation);

	instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, speed ) );

     //      Physics.IgnoreCollision( instantiatedProjectile. collider,transform.root.collider );
        }
    
    
    } 

It works fine when I am moving forward, but when I move left to right, the bullets go all over the place. How can I get them to always move in a straight line?

Try this doc. Jump down to Adding a Weapon.
Creating a First Person Shooter (FPS) - Unity

You should give velocity to projectile according to forward vector of the car.You always give velocity in z axis. Also you should check if any force being applied on projectile or not.