Trying to make a shothun pick up and use ammo clips

OK, I still suck at code even after almost 3 months.
I’m trying to make this script work so that when you pick up a box of shotgun shells it adds the clips to my player GUI
Which it does. The problem I think is in the shotgun script that I’ve been buggering around with for weeks trying to get it to function using clips:
EXAMPLE"
Because it’s a double barrel shotgun it fires two shot.
1 shot BANG!
2nd Shot BANG!

Reload with a new clip or 2 more shells if you prefer :wink:

My ammo gets just added to the GUI string which is just a TEXT GUI that adds the ammo

But once the ammo is added it’s 24 shells plus whatever I still had in the gun… which is OK I guess because atleast you know the full amount of ammo you have.

BUT, And this is the REAL pain in the you know where. My shotgun then will fire with each trigger pull and NOT reload until that ammo amount is gone!?!

Also something else I noticed that I’ve been trying to figure out is, that even if the shotgun has no more ammo and it’s out it will play the “Reload” animation. Which is kinda silly considering it’s got no more ammo to load, so I’ve been scratching my head tryin to figure out how to make that NOT happen as well.

For what it’s worth this is my piece o’crap shotgun script that needs help. :wink:

var range = 70.0;
var fireRate = 4.00;
var force = 300.0;
var damage = 10.0;
var shellsLeft : int = 12;     // How Many clips the player has
var shells : int = 2;            // How many Shells in each clip

var reloadTime = 1.0;
var gunAnimation : Animation;
var reloadSound : AudioClip;
private var hitParticles : ParticleEmitter;
var muzzleFlash : Renderer;

private var nextFireTime = 0.0;
private var m_LastFrameShot = -1;



function Start () {
	hitParticles = GetComponentInChildren(ParticleEmitter);
	
	// We don't want to emit particles all the time, only when we hit something.
	if (hitParticles)
		hitParticles.emit = false;
	shellsLeft = shells;
}

function LateUpdate() {
	if (muzzleFlash) {
		// We shot this frame, enable the muzzle flash
		if (m_LastFrameShot == Time.frameCount) {
			muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360, Vector3.forward);
			muzzleFlash.enabled = true;

			if (audio) {
				if (!audio.isPlaying)
					audio.Play();
				audio.loop = true;
			}
		} else {
		// We didn't, disable the muzzle flash
			muzzleFlash.enabled = false;
			enabled = false;
			
			// Play sound
			if (audio)
			{
				audio.loop = false;
			}
		}
	}
}

function Fire () {
	if (shellsLeft == 0)
		return;
	
	// If there is more than one bullet between the last and this frame
	// Reset the nextFireTime
	if (Time.time - fireRate > nextFireTime)
		nextFireTime = Time.time - Time.deltaTime;
	
	// Keep firing until we used up the fire time
	while( nextFireTime < Time.time  shellsLeft != 0) {
		FireOneShot();
		nextFireTime += fireRate;
	}
}

function FireOneShot () {
	var direction = transform.TransformDirection(Vector3.forward);
	var hit : RaycastHit;
	gunAnimation.Play("Shoot");
	// Did we hit anything?
	if (Physics.Raycast (transform.position, direction, hit, range)) {
		// Apply a force to the rigidbody we hit
		if (hit.rigidbody)
			hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
		
		// Place the particle system for spawing out of place where we hit the surface!
		// And spawn a couple of particles
		if (hitParticles) {
			hitParticles.transform.position = hit.point;
			hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
			hitParticles.Emit();
		}

		// Send a damage message to the hit object			
		hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
	}
	
	shellsLeft--;

	// Register that we shot this frame,
	// so that the LateUpdate function enabled the muzzleflash renderer for one frame
	m_LastFrameShot = Time.frameCount;
	enabled = true;
	
	// Reload gun in reload Time		
	if (shellsLeft == 0)
		Reload();			
}

function Reload () {

	// Wait for reload time first - then add more bullets!
	yield WaitForSeconds(1);
	gunAnimation.Play("Reload");
	audio.PlayOneShot (reloadSound);
	yield WaitForSeconds(reloadTime);
	
	// We have a clip left reload
	if (shells > 0) {
		shells--;
		shellsLeft = shells;
	}
}

function GetShellsLeft () {
	return shellsLeft;
}

Note Don’t get lazy and except people to just do stuff for you after this.

Well your coding was fine but the logic behind it was not. In short you where using your TOTAL ammo not what was loaded in you gun. Anyway I think I fixed your script for you (was hand done though so it might have errors), so you can learn just by looking at and understanding how it works;

var range = 70.0;
var fireRate = 4.00;
var force = 300.0;
var damage = 10.0;
var shellsLeft : int = 12;     // How Many shells the player has
var shells : int = 2;            // How many Shells are loaded
var clipSize = 2;               // How many shells can we fit each time we reload         

var reloadTime = 1.0;
var reloading = false;
var gunAnimation : Animation;
var reloadSound : AudioClip;
private var hitParticles : ParticleEmitter;
var muzzleFlash : Renderer;

private var nextFireTime = 0.0;
private var m_LastFrameShot = -1;



function Start () {
	hitParticles = GetComponentInChildren(ParticleEmitter);
	
	// We don't want to emit particles all the time, only when we hit something.
	if (hitParticles)
		hitParticles.emit = false;
        shells = clipSize;
}

function LateUpdate() {
        if(Input.GetButtonDown("Reload")  shellsLeft > 0)   //set button to whatever you want
                Reload();
	if (muzzleFlash) {
		// We shot this frame, enable the muzzle flash
		if (m_LastFrameShot == Time.frameCount) {
			muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360, Vector3.forward);
			muzzleFlash.enabled = true;

			if (audio) {
				if (!audio.isPlaying)
					audio.Play();
				audio.loop = true;
			}
		} else {
		// We didn't, disable the muzzle flash
			muzzleFlash.enabled = false;
			enabled = false;
			
			// Play sound
			if (audio)
			{
				audio.loop = false;
			}
		}
	}
}

function Fire () {
	if (shells == 0  shellsLeft > 0)
		Reload();
        if (shells == 0 || reloading == true)
		return;
	
	// If there is more than one bullet between the last and this frame
	// Reset the nextFireTime
	if (Time.time - fireRate > nextFireTime)
		nextFireTime = Time.time - Time.deltaTime;
	
	// Keep firing until we used up the fire time
	while( nextFireTime < Time.time  shells != 0) {
		FireOneShot();
		nextFireTime += fireRate;
	}
}

function FireOneShot () {
	var direction = transform.TransformDirection(Vector3.forward);
	var hit : RaycastHit;
	gunAnimation.Play("Shoot");
	// Did we hit anything?
	if (Physics.Raycast (transform.position, direction, hit, range)) {
		// Apply a force to the rigidbody we hit
		if (hit.rigidbody)
			hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
		
		// Place the particle system for spawing out of place where we hit the surface!
		// And spawn a couple of particles
		if (hitParticles) {
			hitParticles.transform.position = hit.point;
			hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
			hitParticles.Emit();
		}

		// Send a damage message to the hit object			
		hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
	}
	
	shells--;

	// Register that we shot this frame,
	// so that the LateUpdate function enabled the muzzleflash renderer for one frame
	m_LastFrameShot = Time.frameCount;
	enabled = true;
	
	// Reload gun in reload Time		
	if (shells == 0)
		Reload();			
}

function Reload () {
        if(shellsLeft == 0)
                return;
        reloading = true;
	// Wait for reload time first - then add more bullets!
	yield WaitForSeconds(1);
	gunAnimation.Play("Reload");
	audio.PlayOneShot (reloadSound);
	yield WaitForSeconds(reloadTime);
	
	// We have a clip left reload
	if (shellsLeft > clipSize) {
		shellsLeft -= (clipSize - shells);
		shells = clipSize;
	}
        else{
                shells = shellsLeft;
                shellsLeft = 0;
        }
        reloading = false;
}

I’ll leave picking up ammo to you, but I should note that when you pick up ammo the amount picked up should be added to “shellsLeft”.

3 months is actually not that long a time to be learning programming, I have bin learning for about a year now (game programming anyway) and am no where near to having mastered it.

P.s. Gave you a bit of a reload upgrade too, so hope you don’t mind lol.

Thanks King, its giving me an error I can’t figure out:
Assets/WeaponScripts/FPSPlayer.js(142,43): BCE0019: ‘GetShellsLeft’ is not a member of ‘Shotgun’.

This is my player script:

// Update Shotgun gui
	// Shotgun gui is simply drawn with a bullet counter text
	if (shotgun) {
		shotgunGUI.text = shotgun.GetShellsLeft().ToString();
	}

I can’t figure out why it’s giving me that error.

3 months feels like forever though :wink: Although I think a lot of that is pure frustration over not being able to solve so many, many problems that keep cropping up… like this one… the technical stuff takes so much time away from the creative stuff which I would rather spend more time on. Oh don’t get me wrong, I totally understand the nescesity for the technical stuff otherwise it wouldn’t work but it’s so complicated for me that it takes all day just to figure out something that to an experienced programmer is a two second fix lol

EDIT

Figured it out, the last bit was missing from your edited script at the very bottom:

function GetShellsLeft () {
	return shellsLeft;
}

Oops my bad. But ya it takes time to master so just keep at it, it gets easier… I hope lol.

Thanks King, A lot of the code looks less complicated than when I first started, so I hope yur right.

Say though. I notice now that on my shotgun that if I hold the fire button down it makes the reload sound play multiple times… like it’s stuttering or something, but not if i just hit fire once for each shot I want to fire… Don’t know why it’s doing that?

When Fire() is called it checks to see if it is out of bullets but still have ammo, and if so reload. But I forgot to make it check if it was already reloading. So once again my bad. Anyway to fix it change the first line of the Fire() function (the first if statement) from;

if (shells == 0  shellsLeft > 0)

To;

if (shells == 0  shellsLeft > 0  reloading == false)

HooAh! That fixed it, you da man! Now I got’s me a sweet ass shotgun :smile: Thanks buddy.
Want some toad stools and dandylion grass texture for your troubles? :smile: