Shooting script not working properly

I’m using this script to shoot:

var bulletPrefab:Transform;

function Update () 
{

 if(Input.GetButtonDown("Fire1"))

   {
     
     //Create bullet
     var bullet = Instantiate(bulletPrefab, GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
  
  //Direction of bullet and add force
  bullet.rigidbody.AddForce(transform.forward * 5000);   

   }
}

But it does not shoot properly. When I turn right it does not shoot, it just creates the bullet in front of the gun. Also when I shoot at certain angles it does not shoot to where I am aiming it shoots to the left. Besides this it works fine. What am I doing wrong?

You need to change “Quaternion.identity”, which aligns the rotation to the world axis, to “transform.rotation” or “spawnPoint.transform.rotation”

and maybe change “transform.forward” to “bullet.transform.forward”

hi!

the spawnPoint is a bit forward the gun and is a child of maincamera?

Hi i was using this script
var bulletPrefab:Transform;

function Update ()
{

if(Input.GetButtonDown(“Fire1”))

{

 //Create bullet
 var bullet = Instantiate(bulletPrefab, GameObject.Find("spawnPoint").transform.position, transform.rotation);

//Direction of bullet and add force
bullet.rigidbody.AddForce(bullet.transform.forward * 5000);

}
}
first of all i cant drag a bullet prefab to inspector of my rifle and it wont shoot