- Home /
How do I make the gun stop shooting when I am out of ammo?
Hi I've been looking around and I cant seem to find a way to make my spawn point for the bullets stop firing if I am out of ammo. I have already scripted my ammo on the gun and I've tried it on the spawn point but the only thing I've found is to Destroy the gameObject. Which obviously cant work because then it would be gone forever in game. What I have on the gun is: var gunHealth : float = 100; private var maxgunHealth : float = 100; private var brokenHealth : float = 30; private var destroyed : float = 0; var broken = false; var unusable = false; var ammo : int = 10; var maxammo : int = 10; private var emptyammo : int = 0; var clipammo : int; private var noclipsammo : int = 0; var empty = false; var reloading = false; var noClips = false; var masteryExp : float = 0; private var leastmasteryExp : float = 0; var maxmasteryExp : float = 100; var levelofMastery : int = 1; var maxlevelofmastery : int = 10; var master = false; var damage : int = 5.5; function Start(){ //need to add the reloading animation animation["trigger"].wrapMode = WrapMode.Once; animation["trigger"].layer = 1; animation.Stop(); }
function Update (){
if (Input.GetMouseButtonUp(0)){
animation.Play("trigger");
}
if (Input.GetMouseButtonUp(0))
gunHealth -= .5;
if (Input.GetMouseButtonUp(0))
ammo -= 1;
if (gunHealth <= brokenHealth){
broken = true;
}
if (gunHealth <= destroyed){
unusable = true;
gunHealth = destroyed;
}
if (gunHealth >= maxgunHealth){
gunHealth = maxgunHealth;
}
if (ammo >= maxammo){
ammo = maxammo;
}
if (ammo <= emptyammo){
ammo = emptyammo;
empty = true;
clipammo -= 1;
}
if (clipammo <= noclipsammo){
clipammo = noclipsammo;
noClips = true;
}
if (masteryExp <= leastmasteryExp){
masteryExp = leastmasteryExp;
}
if (masteryExp >= maxmasteryExp){
levelofMastery += 1;
masteryExp = 0;
}
if (levelofMastery >= maxlevelofmastery){
levelofMastery = maxlevelofmastery;
masteryExp = 100;
master = true;
}
}
function LateUpdate(){ if (broken){ Debug.Log("item is now broken. Get repaired soon"); //I need a GUI message here also that says what is said above. } if (unusable){ Debug.Log("item is now unusable"); Destroy(gameObject); //this is where I need the GUI message to go } if (empty){ Debug.Log("start reloading"); reloading = true; } if (reloading){ Debug.Log("reloading now"); ammo = 10; empty = false; reloading = false; } if (noClips){ Debug.Log("no more ammo available"); ammo = 0; //I need the gun shooting script to stop and I need it to play a clicking sound here when they try and shoot } if (master){ Debug.Log("you are now a master with this weapon"); damage = 35; }
}
and what I have on the spawn point is: var projectile : Rigidbody; var ammo : int = 10; private var maxammo : int = 10; private var leastammo : int = 0; var clips : int; private var noclips : int = 0; private var NoClips = false; private var empty = false; private var reloading = false; var initialSpeed = 20.0;
function Fire () {
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider); }
function Update () { if (Input.GetMouseButtonUp(0)) { Fire(); ammo -= 1; } if (ammo >= maxammo){ ammo = maxammo; } if (ammo <= leastammo){ ammo = leastammo; empty = true; clips -= 1; } if (clips <= noclips){ clips = noclips; NoClips = true; } } function LateUpdate (){ if (empty){ reloading = true; } if (reloading){ ammo = maxammo; } if (NoClips){ ammo = leastammo; Destroy(gameObject); }
}
Thank you for all help this is kind of a noobish question so Im hopeing it will be answered soon.
Answer by SirGive · Mar 14, 2011 at 12:52 PM
You really don't have it set to not fire, and it automatically reloads. try this where you are actually firing:
//if theres no ammo, then the condition won't be met.
if (Input.GetMouseButtonUp(0) && ammo > 0)
fire();
And add an event to keep the reload from triggering automatically.
//add a key event so that it doesn't automatically reload.
if (empty&&Input.GetKeyUp ("space"))
reload = true;
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
AddComponent adds too many (C#) 3 Answers
sending message to bullet (javascript) 1 Answer
bullets are spawning under me and they don't move 1 Answer
Rapid fire Help 1 Answer