bullet prefab always come out in wrong direction

i created a cube with rigidbody as a bullet prefab but then
the bullet prefab always come out in wrong direction what could be wrong? please help

heres my code:

var prefabBullet:Transform;

var shootForce:float;

var shots : int = 0;

var maxShots : int = 8;

var shootSound : AudioClip;

function Update()

{

if(Input.GetButtonDown("Fire1") && shots < maxShots)

{

var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);

instanceBullet.rigidbody.AddForce(transform.forward * shootForce);

Destroy(instanceBullet.gameObject, 5);

audio.PlayOneShot(shootSound);

animation.Play("gunidle");

shots++;

}

else if (shots >= maxShots && Input.GetKeyDown(KeyCode.R))

{

animation.Play("gunshots");

shots = 0;

}

}

in the cube which is the bullet prefab… i put a rigidbody and i freeze its position x and y not the “z” as well as the rotation x, y and z

but then the bullet prefab came out either forward and backward only

If you are just trying to make a bullet come out of gun, I suggest.

  1. Don’t freeze the x and y movements in your prefab.

  2. Put an empty game object just in front of the barrel of the gun. This will be the spawn point for the bullets and (I’ll call it goSpawn).

  3. Rotate the empty object so that it’s rotation matches the barrel of the gun. Note the positive Z direction is forward.

  4. Make the empty game object a child of the gun by dragging it on top of the gun in the Hierarchy view.

  5. When calling Instantiate, use the position and rotation of goSpawn.

    var instanceBullet = Instantiate(prefabBullet, goSpawn.transform.position, goSpawn.transform.rotation);
    instanceBullet.rigidbody.AddForce(transform.forward * shootForce);