need help with my gun script -first try :(

ok so the problem is the gun fires 7 times does the reload animation but when i try shoot the sound of the firing/the bullets dont come out of the gun but the firing animation shows i have tried a bit but cant fix it so im asking if anyone can help :smile: i’ll make a video if that would help more

so basicly the gun doesnt reload with bullets and the shooting sounds stop

english not first language sorry is bad :confused:

var Bullet : Rigidbody;
var Spawn : Transform;
var BulletSpeed : float = 1000;
var ReloadTime : float = 2;
var AmmoInMag : float = 7;
static var AmmoLeft : float;
static var ReloadTTime : float;
static var IsReloading = false;
private var CanFire = true;


function Start () {
 AmmoLeft = AmmoInMag;
 ReloadTTime = ReloadTime;
}

function Update () {
		if(Input.GetButtonDown("Fire1")){
			if(AmmoLeft > 0){
				BroadcastMessage("FireAnim");
				Fire();
			}
			
		}
	
	
	if(AmmoLeft == 0)
	
	{
		Reload();
	}
	
	if(AmmoLeft < 0){
		AmmoLeft = 0;
	}
}

function Fire(){
	if(CanFire == true  IsReloading == false){
		var bullet1 : Rigidbody = Instantiate(Bullet,Spawn.position,Spawn.rotation);
		bullet1.AddForce(transform.forward *BulletSpeed);
		AmmoLeft -= 1;
		audio.Play();
	
	}
	
}

function Reload(){
	CanFire = false;
	IsReloading = true;
	BroadcastMessage("ReloadAnim");
	isReloading = false;
	CanFire = true;
	AmmoLeft = AmmoInMag;
}
    var Bullet : Rigidbody;
    var Spawn : Transform;
    var BulletSpeed : float = 1000;
    var ReloadTime : float = 2;
    var AmmoInMag : float = 7;
    static var AmmoLeft : float;
    static var ReloadTTime : float;
    static var IsReloading = false;
    private var CanFire = true;
          
    function Start ()
 {
     AmmoLeft = AmmoInMag;
     ReloadTTime = ReloadTime;
    }
     
    function Update () {
          if(Input.GetButtonDown("Fire1")){
                if(AmmoLeft > 0){
                    BroadcastMessage("FireAnim");
                    Fire();
                }
else
       
        {
            Reload();
        }
       
     
}
    }
     
    function Fire(){
        if(CanFire == true  IsReloading == false){
            var bullet1 : Rigidbody = Instantiate(Bullet,Spawn.position,Spawn.rotation);
            bullet1.AddForce(transform.forward *BulletSpeed);
            AmmoLeft -= 1;
            audio.Play();
       
        }
       
    }
     
    function Reload(){
        CanFire = false;
        IsReloading = true;
        BroadcastMessage("ReloadAnim");
        isReloading = false;
        CanFire = true;
        AmmoLeft = AmmoInMag;
    }

Try this, basically when you press fire with an empty mag it reloads.

doesnt work has the same effect

it is because IsReloading stays TRUE.

Look very carefully at line 48. You spelled IsReloading wrong. With a lowercase I.

The function should look as follows:

function Reload(){

 CanFire = false;


 IsReloading = true;


 BroadcastMessage("ReloadAnim");


  IsReloading= false;

 CanFire = true;

 AmmoLeft = AmmoInMag;

}

It will play the fire animation because ammoLeft > 0 at this point, so the condition is checked as true, therefore processing the FireAnim argument. It will not, however, play the sound or shoot the bullet, because in the Fire() function, IsReloading is stuck on True.

Quick question. How would it even compile if isReloading is used instead of IsReloading?

I don’t know for sure… I think JavaScript is declaring it as its own local scope public variable of type bool.

sorry, had to rewrite it so that it didnt hurt my head…

#pragma strict

var BulletSpeed:float = 500;
var Bullet:GameObject; // this is a bullet at the end of the gun (attached to the gun), pointing in the direction you want it to fire.

var FireSound:AudioClip;
var ReloadSound:AudioClip;

var reloadTime:float = 2;
var fireRate:float = 120; // shots per minute

var ammoLoaded:int = 20;
var ammoStored:int = 100;
var clipSize:int = 20;
var ammoSize:int = 100;


private var fireTime:float = 0;
private var canFire:boolean = true;

function Start(){
	fireTime = 60 / fireRate;
	Bullet.SetActive(false);
	if(!Bullet.rigidbody) Bullet.AddComponent(Rigidbody);
}

function Update(){
	if(canFire){
		if(Input.GetButtonDown("Fire1")) StartCoroutine(Fire());
		if(Input.GetKeyDown(KeyCode.R)) StartCoroutine(Reload());
	}
}

function Fire(){
	canFire = false;
	var bullet1 : GameObject = Instantiate(Bullet, Bullet.transform.position, Bullet.transform.rotation);
	bullet1.SetActive(true);
	bullet1.rigidbody.velocity = Bullet.transform.forward * BulletSpeed;
	
	ammoLoaded--;
	if(FireSound){
		audio.clip = FireSound;
		audio.pitch = Random.value * 0.2 + 0.9;
		audio.Play();
	}
	BroadcastMessage("FireAnim");
	
	yield WaitForSeconds(fireTime);
	canFire = ammoLoaded > 0;
	
	if(!canFire) StartCoroutine(Reload());
}

function Reload(){
	if(ammoStored == 0) return;
	canFire = false;
	BroadcastMessage("ReloadAnim");
	
	if(ReloadSound){
		audio.clip = ReloadSound;
		audio.pitch = 1;
		audio.Play();
	}
	
	yield WaitForSeconds(reloadTime);
	
	ammoStored += ammoLoaded;
	ammoLoaded = ammoStored >= clipSize ? clipSize : ammoStored;
	ammoStored -= ammoLoaded;
	
	canFire = ammoLoaded > 0;
}

function AddAmmo(n : int){
	var doReload:boolean = ammoStored == 0  ammoLoaded == 0  n > 0;
	ammoStored = Mathf.Clamp(ammoStored + n, 0, ammoSize);
	if(doReload) StartCoroutine(Reload());
}