How to make bullet spawn to rotate pivot with spaceship?

I am beginner and 3d artist! I making Space Shooter game in JavaScript with camera from bird-view, spaceship can turn around with “AddForce” and i have problem with intantiation bullets from spawner(empty game object) that i created for shooting! When i turn around my SpaceShip spawner dont rotate along ship so i can shoot straight only in one world z direction… how can i make him to shoot where my spaceship look at?

var speed : float = 10;
var rotationSpeed: float = 10;
var bullet : GameObject;
var bulletSpawn : Transform;
var sideForce : float = 20;
var topSpeed : float = 20;

function Update()
{
	//Controls for ship...
	if(Input.GetKey("s") || Input.GetKey("0"))
	{
		rigidbody.AddRelativeForce(Vector3.back * speed * Time.deltaTime);
	}
	if(Input.GetKey("w") || Input.GetKey("1"))
	{
		rigidbody.AddRelativeForce(Vector3.forward * speed * Time.deltaTime);
	}
	
		if(Input.GetKey("q") || Input.GetKey("0"))
	{
		rigidbody.AddRelativeForce(Vector3.left * sideForce * Time.deltaTime);
	}
	
		if(Input.GetKey("e") || Input.GetKey("0"))
	{
		rigidbody.AddRelativeForce(Vector3.right * sideForce * Time.deltaTime);
	}
	
		if(Input.GetKey("a"))
	{
		transform.Rotate(Vector3.down * rotationSpeed * Time.deltaTime);
	}
	
		if(Input.GetKey("d"))
	{
		transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
	}
	
		
	//SHOOTING...
		if (Input.GetKey("f"))
	{
		Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation);
	}	
	
	//unsuccessful attempts to make braking sistem on spaceship (speed slowing down)
	if (rigidbody.velocity.magnitude > topSpeed)
    rigidbody.velocity = rigidbody.velocity.normalized * topSpeed;
    
    transform.eulerAngles.x=0;
    transform.eulerAngles.z=0;
    transform.position.y=0;
    
    if(Input.GetKeyDown(KeyCode.LeftShift))
    {
        rigidbody.velocity = Vector3.zero;
    }
}

First, make your bullet object into a prefab. (I am assuming your bullet is already scripted to do damage and all those things, so I won’t go into that). Attach a script to your bullet prefab (call it whatever you want). Create an int variable called “speed”, define it as whatever you want. Put this line in the update function:

rigidbody.AddForce (Vector3.forward * speed);

You can use the speed variable to change how fast your bullet will move. Then attach this script to the bullet prefab.

It looks like you’ve already got a way to instantiate the bullet, so this should work. Although, to avoid glitching out your game, you may want to also add a timer to the bullet script that destroys the bullet after a certain time.

Edit: Also, as robertbu pointed out, this will only work if the bulletSpawn is a child of the spaceship.

I believe if you make the following change it will fix your problem. Change this:

transform.position.z += bulletSpeed * Time.deltaTime; 

…to this:

transform.position += transform.forward * bulletSpeed * Time.deltaTime;

There may be other issues, but start with this change.