Bullet Rotation faces wrong direction

My shooting script does what it needs to do in terms of shooting a rigidbody bullet forward , but the bullet always faces the Z axis. I don’t want the bullet to always face the Z axis, I want it to face forward as normal bullets do. Here’s an example…

My code here…

#pragma strict
@script RequireComponent(AudioSource)
var soundeffect : AudioClip;
var soundeffect2 : AudioClip;
var Effect : Transform;
public var TheDamage = 50;
var shootdistance : int = 500;
var bullets : int;
var fullmag : int = 7;
var reloadtime : float = 2.8;
var onetime : boolean = false;
var timer : float = 0;
var fireRate : float = 0.15;
var Spawn : Transform;
var bulletPrefab : Transform;
var shootForce : float = 300;

function Start()
{
	bullets = fullmag;
	light.intensity = 0;
}

function Update () {
	timer += Time.deltaTime;
	
	if (timer > fireRate){
		if (Input.GetButtonDown("Fire1")&& bullets > 0){
			bullets --;
			timer = 0;
			audio.clip = soundeffect;
			audio.Play();
			light.intensity = 4;
			Invoke ("lightoff",0.25);
			fire();
			var particleClone = Instantiate(Effect, Spawn.position, Quaternion.identity);
			Destroy(particleClone.gameObject, 1);
		}
	}
	if (bullets <= 0 && !onetime){
		audio.Stop();
		audio.clip = soundeffect2;
		audio.Play();
		Invoke ("reload",reloadtime);
		onetime = true;
	}
}

function fire(){
	audio.clip = soundeffect;
	audio.Play();
    var instanceBullet = Instantiate(bulletPrefab,Spawn.position, Quaternion.identity);
   	bulletPrefab.rigidbody.useGravity = false;
    instanceBullet.rigidbody.AddRelativeForce(transform.forward*shootForce);
}

function lightoff(){
	light.intensity = 0;
}

function reload(){
	bullets = fullmag;
	Invoke("canreload",reloadtime);
}

function canreload(){
	onetime = false;
}

function OnGUI()
{
	if ( bullets > 0){
		GUI.Box( Rect( (Screen.width/2)-100, 10, 200, 25 ), "Ammo: " + bullets + "/" + fullmag);
	}
	else{
		GUI.Box( Rect( (Screen.width/2)-100, 10, 200, 25 ), "Reloading" );
	}
}

var instanceBullet = Instantiate(bulletPrefab,Spawn.position, Quaternion.identity);
Quaternion.identity is the default rotation of the bullet, you need to rotate the bullet.

var rotation = Quternion.FromToRotation(bulletPrefab.forward, Spawn.position.forward);
var instanceBullet = Instantiate(bulletPrefab,Spawn.position, rotation);

have not tried the code, but something like this should work.

I recommend getting the player rotation at the time of firing, and scripting, at the creation of the bullet object, the rotation to be that of the player.

Please try replacing your ‘function fire()’ with mine, as shown below. Please tell me if you have problems with assigning the bullet prefab to the script, and make a copy of your original function.

It is important to attach the script to the object that you want it to get its rotation axes from.

You will notice that I have commented out the ‘Quaternion.identity’ part of the code. That’s because I didn’t need it, but if you do need it, please un-comment it.

function fire(){
	audio.clip = soundeffect;
	audio.Play();
	var instanceBullet = Instantiate(bulletPrefab, transform.position, transform.rotation/*, Quaternion.identity*/);
		bulletPrefab.rigidbody.useGravity = false;
	instanceBullet.rigidbody.AddRelativeForce(transform.forward*shootForce);
}

If there are any problems, please tell me.