- Home /
limited ammo and ammo pickups
In my splitscreen shooter instead of having to pick up weapons, each plaer has all the weaons at the start but so far all the guns have unlimited ammo, I am using an automatic rigidbody shooting script that is added to an empty prefab child in each weapon prefab called 'firing point', I want to change my shooting script to have a limited supply of held ammo. So basically when a weapon's ammo has depleated I want it to disable the firing point child so the player cant shoot it anymore and then when the player comes into contact with an ammo pickup I want to tell the ammo pickup to send a message to the shooting script to add ammo to the weapon and re-enable the firing point child so the player can use the weapon again.
here's my script
RequireComponent(AudioSource); var audioSource: AudioSource; var Projectile : Rigidbody; var ProjectileSpeed : int = 10; var FireRate : float = 10; // The number of bullets fired per second var lastfired : float; // The value of Time.time at the last firing moment var gunshot : AudioClip; var muzzleflash :Transform;
function Start() { audioSource = GetComponent.(); }
function Update () { if (Input.GetAxis("Fire1")>0f) { if (Time.time - lastfired > 1 / FireRate) { lastfired = Time.time; var clone : Rigidbody; clone = Instantiate(Projectile, transform.position, transform.rotation); clone.velocity = transform.TransformDirection (Vector3.forward * ProjectileSpeed); audioSource.PlayOneShot(gunshot, 0.7F); Instantiate (muzzleflash,transform.position,transform.rotation); } } }
Answer by AbominationGames · Aug 20, 2017 at 05:51 AM
You technically don't need to disable the firing point. You can create a boolean canShoot and set it to false or true. Then you can just toggle the boolean, this will also allow for you to add to your script later, (weapon jams, reloading, hit, object interference, etc).
PSEUDO CODE: ammo = 0; canFire = false;
Update: if ammo >= 0 canFire = true
if (canFire) { if(Input.GetAxis("Fire1") && Time.time - lastfired > 1 / FireRate) { Shoot();
Shoot() { InstantiateBuller(); ResetShotTimer(); }
So this way you never have to disable your fire point you just can't shoot whenever your bool is set to false. This allows you to make it to where if you're falling, or in the water later you can't shoot. It will allow you to expand later on this way much easier than having to deal with disabling the objects.
@gamemonk
what I want to do is make it similar to turok rage wars on n64, where the player starts with some ammo for their first weapon (for my case the pistol)
that boolean looks like what I need but coudl you please add it into my script in my answer because I don't quite know where to put your code snippets
Your answer
Follow this Question
Related Questions
how do i make this advance fire system for my player!!! 1 Answer
Repeatedly Getting Errors On An Ammo Script 3 Answers
Display Ammo Count Above Player 1 Answer
Gun with limited ammo? 1 Answer
Projectile Firing/Gun script 1 Answer