- Home /
Question by
Karl Kujak · Jul 05, 2010 at 12:00 AM ·
guishootingcountammo
adding ammo count
What would be the code to use if i want to limit how many times my player can shoot and how do i make it appear on the screen as a GUI
Comment
Answer by spinaljack · Jul 05, 2010 at 12:09 AM
function Fire(){ if(ammo>0){ //shoot } }
function OnGUI(){ GUI.Label(Rect(someRect),"Ammo : " + ammo); }
Answer by Unity_scat · Sep 25, 2015 at 08:03 AM
I updated the script. It's more reliable now, and you can't reload if currentammo is equal or grater than maxammo.
var canshoot = true;
var canreload = true;
var currentammo : int = 5;
var maxammo : int = 15;
function Update () {
if(currentammo == maxammo) {
canreload = false;
}
if(currentammo > maxammo) {
canreload = false;
}
if (Input.GetKeyDown("1")) {
Shoot();
}
if (Input.GetKeyDown("2")) {
Reload();
}
if(currentammo == 0) {
canshoot = false;
}
if(currentammo > 0) {
canshoot = true;
}
if(maxammo == 0) {
canreload = false;
}
if(maxammo > 0) {
canreload = true;
}
}
function OnGUI () {
GUI.Box(new Rect(5, 105, 105, 23), "Ammo: "+currentammo+"/"+maxammo);
}
function Shoot () {
if(canshoot == true) {
if (Input.GetKeyDown("1")) {
if(currentammo > 0) {
currentammo -=1;
}
}
}
}
function Reload () {
if(canreload == true) {
if(Input.GetKeyDown("2")) {
currentammo +=1;
maxammo -=1;
}
}
}
Your answer
Follow this Question
Related Questions
limited ammo and ammo pickups 1 Answer
Display Ammo Count Above Player 1 Answer
gui help for ammo display 1 Answer
the count remains the same 0 Answers
2D FPS shooting 1 Answer