Problems with limited ammo....

Hi, Im now working on giving my gun limited ammo, but is not working correctly:

var fire : AudioClip; 
var reload : AudioClip;
var shooting : boolean = false; 
var reloading : boolean = false;
var crosshairTexture : Texture2D;
var ammoCount : int = 17;
var position : Rect;
private var clipCount : int = 1;

function Start()
{
	position = Rect( ( Screen.width - crosshairTexture.width ) / 2, 		( Screen.height -
	crosshairTexture.height ) / 2, crosshairTexture.width, 					crosshairTexture.height );
}

function OnGUI()
{
GUI.DrawTexture( position, crosshairTexture );
}

function Update ()
{ 
      if(Input.GetButtonDown("Reload")  !reloading || ammoCount < 1  clipCount > 1) { 
      	
      	Reload();
      	
      	clipCount =- 1;
      }
      
      if (Input.GetKey("w"))
      Walk();
      if (Input.GetKey("s"))
      Walk();
	  if (Input.GetKey("a"))
	  Walk();
      if (Input.GetKey("d"))
      Walk();   

   
    
   else; 
   
   if(Input.GetButtonDown("Fire2")) {
   	animation.Play("Melee");
   }

   if(Input.GetButtonDown("Jump")) {
   	animation.Play("Jump");
   }
   if(Input.GetButtonDown("Flashlight")) {
   	animation.Play("Flashlight");
   }

   if (Input.GetButtonDown("Fire1")  !shooting  ammoCount > 1) {
   	Fire();
      
      ammoCount =- 1;
   }   
   	
}  

function Fire() {
	  animation.Play ("Fire"); 
      audio.PlayOneShot (fire); 
      shooting = true; 
      
      if(!animation.IsPlaying ("Fire")) { 
      shooting = false; 
      }
}         
function Reload() {
	   animation.Play("Reload");
       audio.PlayOneShot(reload);
       reloading = true;
   
   if(!animation.IsPlaying ("Reload")) {
   	reloading = false;
   }
}

function Walk()
{
animation.Play("Walk");
	}

Problems:

  1. Some how it sees that after I reload, the clipCount is less than 1, and wont do it again…
  2. also, it never stops firing to relaod or just quit…
  3. The fire sound is played every frame in the fire animation, not once, witch the var shooting is supposed to fix, it used to, but not any more…

Any tips? Corrections? Info on int variables? THX!! :smile: :?

You have

   clipCount =- 1;

You mean

   clipCount -= 1;

Same for your ammunition count.

Wherever you put “=- 1” you’re telling it to assign a value of “-1”.

Some of those functions don’t seem to make much sense. They tell Unity to play an animation, then immediately perform a test to see if the animation is playing. I’d try something along the lines of this:

if(Input.GetButtonDown("Reload")) {
   Reload(); 
}

if (Input.GetButtonDown("Fire1")) {
   Fire();
}
function Reload() { 
   if(!interactingWithWeapon  ammoCount < bulletsInClip  clipCount > 0) { 
      animation.Play("Reload"); 
      audio.PlayOneShot(reload); 
      interactingWithWeapon = true;
      yield WaitForSeconds( reloadDelay );
      ammoCount = bulletsInClip;
      clipCount--;
      interactingWithWeapon = false;
   }
}
function Fire() { 
   if (!interactingWithWeapon  ammoCount > 0) {
      animation.Play ("Fire"); 
      audio.PlayOneShot (fire); 
      interactingWithWeapon = true; 
      ammoCount--;
      // Instantiate projectiles, cast rays etc.
      yield WaitForSeconds( fireAgainDelay );
      interactingWithWeapon = false;
   }
}

Please note: I’ve assumed extra variables which you haven’t included. They should be properly declared and given appropriate values. Which ones they are, what they are for, and what types they are, should be obvious from context.

If you’d like to make the delays equal to the length of the animation, start here: Unity - Scripting API: Animation.clip This should point you in the right direction for getting the length of an animation clip.

Wow, I havent posted a reply yet? :? weird…
But thanx, after a little tweaking, it still needs a lot of tweaking… lol

But the problem I have now that I want to fix is that the clipCount goes negative when I reload. How do I disable the reload function if the clipCount is 0? :smile:

Put this in the reload function:

clipCount = 1; //Or whatever the default for a new clip is

But where? Im getting really confused…

Anywhere in the function is fine. Right at the start.

Just slap it into the Reload() function and it will update when the graphics do.

EDIT:
Actually the reload function given to you by DMJ there properly reloads it. Mine wouldn’t have done what you desired, I mis-read your code.
DMJ’s code should work for you fine.

Hmm, thanks, it works! But what about automatic reloading, so if the clipCount is less than 1, it will automaticlly relaod… also, I want to change my animation so it looks like the player is dropping the clip. This way if theres say, 16 rounds left and he reloads, it will be a full clip but will still deduct a clip like he just forgot the clip. And also the HUD. I needa speend some time with the gui system! :smile: Now im going to work on picking up clips, Ive never had to access a script before, so this will be tricky! :roll:

Ok, problem with this pickup script:

var clip : AudioClip;

function OnControllerColliderHit (hit : ControllerColliderHit) {
	if (hit.gameObject.tag == ("GlockClip")) {
		Destroy (gameObject);
		audio.PlayOneShot (clip);
		script = GetComponent(Glock17);
		Glock17.PickUp();
	}
}

The console says:

What does it mean by instance? BTW, I added a function called PickUp() to my Glock17 script. But, what am I doing wrong here?

function Fire() { 
   if (!interactingWithWeapon) {
      if (ammoCount > 0){
         animation.Play ("Fire"); 
         audio.PlayOneShot (fire); 
         interactingWithWeapon = true; 
         ammoCount--; 
         // Instantiate projectiles, cast rays etc. 
         yield WaitForSeconds( fireAgainDelay ); 
         interactingWithWeapon = false; 
      }
      else {
         Reload();
      }
   } 
}

*Edited for elegance.

It might help to clarify things slightly here:

clipCount is how many clips the player is carrying. ammoCount is how many rounds are in the current clip. interactingWithWeapon is set to true whenever the weapon should be unavailable (like when reloading or in the “down time” between shots). This could also be set true while changing weapons, moving the camera to look down sights etc. to prevent a weapon being used while the animation is being played.

You have told Unity to call the function “PickUp()” on “Glock17”. Unity doesn’t know which Glock17 to apply it to. Your code assigns the Glock17 script to the variable named “script”, so you talk to it by saying this particular instance’s variable name, not the script’s own name:

   script.PickUp();

Incidentally, the name “script” for a variable is really unhelpful. You might want to change it to something a lot more descriptive. Descriptive variable names cost nothing, mystery variables you can’t remember the meaning of cost frustration and time working out what you meant in the first place!

It’s just occurred to me that you might be thinking of the ammunition as a “pool” of bullets, rather than a number of clips that get discarded on reload. If that’s the case, try this:

function Reload() {
   if (!interactingWithWeapon  ammoInGun < clipSize  ammoInPool > 0) {
      animation.Play("Reload"); 
      audio.PlayOneShot(reload); 
      var bulletsRequired : int = clipSize - ammoInGun;
      bulletsRequired > ammoInPool ? bulletsRequired = ammoInPool;
      interactingWithWeapon = true;
      yield WaitForSeconds( reloadDelay );
      ammoInGun += bulletsRequired;
      ammoInPool -= bulletsRequired;
      interactingWithWeapon = false;
   }
}

Yes, I know I’ve changed some variable names in the examples I’ve posted :smile: Hopefully it shouldn’t be too hard to work out which ones they are and what they do.

Okay, final edit for now:

I’ve thought a bit about your pickup script, and this is how I’d do it. I’d give the ammo object on the floor a script like this:

var soundOfGlockAmmoPickup : AudioClip; 
public var ammoInThisObject : int = 8;

function OnCollisionEnter ( collision : Collision ) { 

   // Loop through everything colliding
   for ( var contact in collision.contacts ) {

      // If the current colliding object is the Player...
      if (contact.gameObject.tag == ("Player") {

         // We will talk to the Player's Glock17 script through "glock".
         glock = contact.GetComponent(Glock17);

         // If the player isn't on full ammo...
         if (glock.ammoInPool < glock.maxAmmoInPool) {

            audio.PlayOneShot(soundOfGlockAmmoPickup);
            glock.AddAmmo(ammoInThisObject);

            // This object is no longer needed.
            Destroy( gameObject );

         } // End if (glock.ammoInPool < glock.maxAmmoInPool)

      } // End if (contact.gameObject.tag == ("Player")

   } // End for ( var contact in collision.contacts )

}

I’d make sure that the Glock17 script on the player has a function called AddAmmo( ) which accepts an int and adds that int to the ammo pool for that weapon (and clips it to the max ammo value if it hasn’t got

This is in a completely untested state, I’d normally run this through Unity a few times and squash error messages as they appear and probably rewrite it half a dozen times, but I think this should point you in the right direction.

Hmm, ive reached a bit of a snag with the pick up clip script. The way its set up is so that one script is applied to the character controller:

//Apply to CC, Tag clip with "GlockClip"

var clip : AudioClip;
var clipToDestroy : GameObject;

function OnControllerColliderHit (hit : ControllerColliderHit) {
	if (hit.gameObject.tag == ("GlockClip")) {
		ClipPlus();
	}
}

function ClipPlus()
{
	var object = GameObject.FindWithTag("GlockClip");
	Destroy (clipToDestroy);
	audio.PlayOneShot (clip);
	script = GetComponent(Glock17);
	script.PickUp();
	}

Thats the problem: One script, several clips in the scene. So how and what do I need to change to apply the script to the clip itself, not the CC?
Also, It never gives me one extra clip in my Glock17 script, instead the console returns:

Damn, thats a long error! But I cant understand it, what does this script have to do with Boo? Its JS. Any answers? 8)

Ignore that. It is simply saying that you are referring to a pointer for your Object but it is null, ie your Object isnt there.
Which line is line 25? That is where the problem lies in that script.

Did tweaking the last code snippet in my last post above not work?