Gun reloading problem...

Hi, I have this script that I have been working on, and the problem is that I can’t get it to either put 30 rounds in the clip and subtract 30 from the cache, and to simply just put 30 in the clip or less based on if theres less in the cache. Im kindof confused now, and if anyone can point me to what I can do to do this, please help.

var isInteracting : boolean = false;

var ammoCache : int = 60;
var ammoClip : int = 30;

var fireRate : float = .2;
var timeToReload : float = 3;

var fireSound : AudioClip;
var reloadSound : AudioClip;

private var ammoHold : int;
private var cache : int;
private var clip : int;

function Update () {
	
	cache = ammoCache;
	clip = ammoClip;
	cache += clip = ammoHold;
	
	
		if (Input.GetButtonDown("Reload")  isInteracting == false  ammoClip <= 30  ammoCache >= 0 || ammoClip <= 0  ammoCache >= 1) {
		Reload();
		}
		
	if (Input.GetButton("Fire1")  isInteracting == false  ammoClip >= 0) {
		Fire();
	}  else if (!animation.isPlaying) {
		animation.Play ("Neutral");
	}
	
	if (ammoClip <= 1) {
		isInteracting = true;
	}
}

function Fire() {
		isInteracting = true;
		audio.PlayOneShot (fireSound);
		animation.Play ("Fire");
		ammoClip --;
		Debug.Log (ammoClip);
		yield WaitForSeconds (fireRate);
		isInteracting = false;
		
}

function Reload() {
	if (ammoCache <= 29  ammoHold <= 30) {
		isInteracting = true;
		ammoClip = ammoCache;
		audio.PlayOneShot (reloadSound);
		animation.Play ("Reload");
		ammoCache = 0;
		yield WaitForSeconds (timeToReload);
		isInteracting = false;
		Debug.Log (ammoCache);
		
	} else if (ammoCache <29  ammoHold >= 30 || ammoCache <= 30  ammoHold >= 30) {
		isInteracting = true;
		audio.PlayOneShot (reloadSound);
		animation.Play ("Reload");
		ammoClip = 30;
		ammoCache -= ammoHold;
		yield WaitForSeconds (timeToReload);
		isInteracting = false;
		Debug.Log (ammoCache);
		
	}
}

//static function ClipPlus() {
	//ammoCache += 30;
	//}
@script RequireComponent (AudioSource)

Also, heres the gun model. Thanks.

277420–9950–$ak_47_modeljas_141.zip (28.1 KB)

Hi! I’m sort of confused by reading your script!

You seem to be dealing with three variables when you only need two. ammoHold is derived - if you ever want to know how many bullets total you have, you can use ammoCache + ammoClip - it will make your code less error prone too.

I kind of mooshed your Reload() function around to do what I assume it should do, the logic being to first to check if the gun could be reloaded, then check if the clip can be fully reloaded, then to do the animation and sound effects stuff.

function Reload() { 
   if (ammoClip < 30  ammoCache > 0) { //check the gun isn't fully loaded and we have spare bullets
      if (ammoCache >= (30 - ammoClip) { //we have enough bullets for a full reload
         ammoClip = 30;
         ammoCache -= (30 - ammoClip);
      } else { //we can only do a partial reload
         ammoClip += ammoCache;
         ammoCache = 0;
      }

      //common actions
      isInteracting = true; 
      audio.PlayOneShot (reloadSound); 
      animation.Play ("Reload"); 
      yield WaitForSeconds (timeToReload); 
      isInteracting = false; 
      Debug.Log (ammoCache);
   }
}

Hope it helps!

Thanks! I was getting super confused on how to solve this, and I compleatly neglected comments, which they keep you in order in what does what, so that could be a problem, lol. And thanks for your fast response! :smile:

Oop, found a bug! You put:

 ammoClip = 30;
         ammoCache -= (30 - ammoClip);

And it should be:

         ammoCache -= (30 - ammoClip); 
         ammoClip = 30;

If its not like that, it will always add 30 to the ammoCache and 30 to ammoClip, which will be unlimited ammo… lol :smile: :smile: :smile: