Weapon Class Need some advice

Hi guys and thanks for look the problem i am having is. I have created a weapon class and need to have a delay between shots, decide which gun is selected, i am new to classes but can programme with ease this being the exception.

var gunList : Gun[];



class Gun

{

	var name : String;

    var isSelected :  boolean;

    var canUserShoot :  boolean;

    var shootForce :  float;

    var ammo : int;

    var bullet :  GameObject;

    var bulletHolder : Transform;

    var shootSound : AudioClip;

    var cost : int;

    var unlocked : boolean;

    var muzzleFlash : Texture2D;

    var shootEffect : Transform;

}



function Update()

{

	if(Input.GetButton("Fire1")  Gun.rayGun.canUserShoot == true)

		{

			RayGun();

		}

}





function RayGun()

{

	for(var rayGun : Gun in gunList)

		{

			Debug.Log("We are shooting");

			rayGun.canUserShoot = false; // can we shoot

			Debug.Log(rayGun.canUserShoot);		

			animation.CrossFade("Gun");

			yield WaitForSeconds(2);

			//AudioSource.PlayClipAtPoint(bulletSoundGun, transform.position);//AUDIO

			var shoot = Instantiate(rayGun.bullet, rayGun.bulletHolder.position, rayGun.bulletHolder.localRotation); // instantiate the bullet at the bulletHolderGunCam's position and rotation

			shoot.rigidbody.AddForce(transform.forward * rayGun.shootForce);//add force to the bullet prefab // BULLET \\

			rayGun.ammo -= 1; // take 1 away from our ammo

			rayGun.canUserShoot = true; // we can now shoot again;

			Debug.Log("We are at the end of the ray gun " + rayGun.canUserShoot);

		}

}

You want to look at InvokeRepeating

Here is a quick example

function Update () {

	if (Input.GetButtonDown ("Bombs")  AllowBomb) {
		...
		AllowBomb = false;
		InvokeRepeating( "AllowBombsAgain" , TimeBetweenBombs , 0 );
	}
}

function AllowBombsAgain (){
	AllowBomb = true;
	CancelInvoke( "AllowBombsAgain" );
}

So how does this work like why would i invoke repeating?

Because you would disable firing of the weapon when it shoots. The Invoked function would allow the weapon to be fired again.

Oh, no… not going to let you guys boost him down the “InvokeRepeating” path. It is completely unnecessary if you just handle time correctly.

OK, first, you need to think about how this is done. You need a gun, but that gun needs to be a GameObject, not a class. So rewriting it as if the Gun were a script instead of a class. This allows you to use the same “guns” array, but then you just drag and drop the guns onto the list from the scene or asset database.

Next, you have started on a gun shooting script, but you need a better way to handle the implimentation. Too many variables makes it a mess.

So, lets start off with the GunInventory. You can add a bit more to this. It is basic. There is a reload function later on, but no connection to a key as of yet.

// GunInventory.js
var gunList : Gun[];
var gun : Gun[];
var currentGun : int = -1;
var gunSwapTime = 0.5;

function Start(){
	if(guns.Length == 0) return;
	for(var i = 0; i < guns.Length; i++){
		guns[i].gameObject.active = false;
	}
	gun = guns[0];
	guns[0].gameObject.active = true;
	currentGun = 0;
}

function Update(){
	if(guns.Length == 0) return;
	if(Input.GetButton("Fire1")){
		if(gun){
			if(gun.Shoot();){
				animation.CrossFade("ShootGun");
			}
		}
	}else{
		var c = currentGun;
		if(Input.GetKeyDown(KeyCode.Alpha1)) c = 0;
		if(Input.GetKeyDown(KeyCode.Alpha2)) c = 1;
		if(Input.GetKeyDown(KeyCode.Alpha3)) c = 2;
		if(Input.GetKeyDown(KeyCode.Alpha4)) c = 3;
		if(Input.GetKeyDown(KeyCode.Alpha5)) c = 4;
		if(Input.GetKeyDown(KeyCode.Alpha6)) c = 5;
		if(Input.GetKeyDown(KeyCode.Alpha7)) c = 6;
		if(Input.GetKeyDown(KeyCode.Alpha8)) c = 7;
		if(Input.GetKeyDown(KeyCode.Alpha9)) c = 8;
		if(Input.GetKeyDown(KeyCode.Alpha0)) c = 9;
		
		if(c != currentGun  c < guns.Length){
			gun = guns[c];
			gun.gameObject.active = true;
			gun.transform.parent = guns[currentGun].transform.parent;
			guns[currentGun].gameObject.active = false;
			currentGun = c;
			
			gun.nextShot = Time.time + gunSwapTime;
			animation.CrossFade("SwapGuns");
		}
	}
}

As you can see… it is very basic, it just manages a current and inventory of guns. It is even linked to the keys 1 to 0 if you have that many of them. You can also see that if you press the left mouse button, it tries to “shoot” the gun. This is handled in the Gun script. It returns a true or false, but should be set up to return a string. The string would be the animation associated with the action. So, if it reloads, it would play the reload animation, if it shot a heavy gun, it would play a heavy shot, light, light and so on. Customization across animations would be a really nice thing to have.

On to the gun script.

//Gun.js
var name : String;
var nextShot :  float; // time based calculation for next shot
var bulletSpeed :  float; // changed due to use of velocity
var ammo : int; // total ammo remaining
var clipSize : int; // max size of the clip
var clip : int; // current ammo in clip
var rateOfFire : float;
var reloadTime : float;
var cost : int;
var unlocked : boolean;

var bullet :  GameObject;
var shootSound : AudioClip;
var reloadSound : AudioClip;
var emptyShotSound : AudioClip;
var muzzleFlash : Texture2D;
var shootEffect : Transform;

function Start(){
	if(!audio)
		gameObject.AddComponent(AudioSource);
}

function Shoot() : boolean{
	if(Time.time < nextShot) return;
	if(clip == 0  ammo == 0){
		if(clip == 0) audio.PlayOneShot(emptyShotSound);
		nextShot = Time.time + rateOfFire;
		return false;
	}
	if(clip == 0){
		Reload();
		return false;
	}
	var bt = bullet.transform;
	var newBullet = Instantiate(bullet, bt.position, bt.localRotation); // instantiate
	newBullet.rigidbody.velocity(bt.forward * bulletSpeed);
	audio.PlayOneShot(shootSound);
	clip--;
	nextShot = Time.time + rateOfFire;
	return true;
}
	
function Reload(){
	if(clip == 0  ammo == 0) return;
	ammo += clip;
	clip = 0;
	clip = ammo > clipSize ? clipSize : ammo;
	ammo = ammo > clipSize ? ammo - clipSize : 0;
	audio.PlayOneShot(reloadSound);
	nextShot = Time.time + reloadTime;
}

Here we house all of the recording of when something can shoot, how much ammunition it has, clips and so forth. Since this is now a game object, you can have sounds and stuff attached to it. Much broader use then the class. And much simpler then InvokeRepeating… Why the heck would you keep repeating?

The premise of why this is better. Because you are not guessing if the invoke is actually repeating. The way it was written simply is an invoke… after all, as soon as you invoke it… you cancel the invoke… Big waste of time IMO.

If you hold down the left mouse button, it will shoot every chance it gets, then reload. So that does exactly what you are looking for. :wink: