Instantiating Weirdly

I have a gun that is shooting bullets that spray the longer you shoot ( this question is the bases for what I’m doing FPS Gun Accuracy & Bullet Tracers - Questions & Answers - Unity Discussions). the only problem I have is that when I turn, the bullets start going spawning sideways from the gun until they reach about a 90 degree angle then they start coming back to the front of the gun. If I keep turning the same direction they move farther away from the gun again until they are at about the opposite 90 degrees again. If i keep rotating they move back to center and this keeps repeating. Something similar to a sine or cosine. Here is the script on the gun:

public string weaponName;
	public GameObject bullet;
	public Rigidbody instantiatedProjectile;
	public int speed;

void shoot(float shotSpread ){
		
		float vx = (1f - 2f * Random.value) * shotSpread;
		float vy = (1f - 2f * Random.value) * shotSpread;
		float vz = 1.0f;
		
		Vector3 direction = transform.TransformDirection(new Vector3(vx,vy,vz));
		
		GameObject bulletClone = Instantiate(bullet, transform.position, transform.rotation) as GameObject ;
		bulletClone.SendMessage("direction",direction);
		audio.Play();
		
	}

and on the bullet:

public Vector3 dir;
	void Update () {
	
		
		transform.Translate(Vector3.forward + dir);
		
	}


	void direction(Vector3 direction ){
		
		dir = direction;
	}

the player has a script that all it does is send the shotSpread and calls the shot function on the gun when it shoots and shotSpread is just a float that increases as you hold the trigger. Pretty sure the problem isn’t there so I’ve left it out for now. The gun is also a child of the camera so it follows it around. What is going on? is it with the Vector3.forward on the bullet? Any ideas for a workaround or a fix? Thanks in advance!

Couple things that need fixing.

First, SendMessage is a little expensive, you can set the function direction as public (or even better, make it a property) and then call bulletClone.GetComponent().direction = …

Secondly, I doubt that the user is going to see the rotation of the bullet, unless it’s reeeeeeeally slow, but for learning purposes you should make it look at that direction (LookAt, LookRotation, or forward =).

Then, you need to travel in that direction every frames. Vector3.forward is (0,0,1) in world coordinate, so when you turn it’s lost. It goes like that (not finale version) :

private void Update(){
    transform.Translate( tranform.forward ); // If you did the rotation
    //transform.Translate( dir); // If you didn't, make sure it's normalized.
}

But this code is dependant of the frame rate, which mean it will travel really fast in good computers and really slow on shitty ones. To fix that, you need to multiply the vector by the delta time and a var to control it.

private void Update(){
    transform.Translate( tranform.forward * Time.deltaTime * speed );
}

Finaly, know that you won’t have collision detection that way. Do it, and when it looks nice look the doc for rigidbody.