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
function Fire(){
if(ammo>0){
//shoot
}
}
function OnGUI(){
GUI.Label(Rect(someRect),"Ammo : " + ammo);
}
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;
}
}
}