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" );
}
}