I have created a script using Unityscript that makes a gun fire a certain amount of bullets everytime you pull the trigger and then requires a cooldown before being able to fire again however sometimes more than the specified amount of bullets are shot and it seems the script is not counting the first shots that are fired.
I have tried moving around the order of the commands however nothing seems to be happening. Here is my script
function Update () {
if (Input.GetButton("Fire1") && Time.time > nextFire && canFire == true){
roundsFired++;
//Instantiate bullet and add force here
}
if (roundsFired >= roundsPerBurst){
canFire = false;
weaponCooldown();
}
else{
canFire = true;
}
}
function weaponCooldown() {
yield WaitForSeconds (CooldownTime);
roundsFired = 0;
}
Any help is appreciated.
Zac Yeates
Have you tried to set up some debug logs to see what roundsfired , canfire states you have ?
also what i would change is
function Update () {
if (Input.GetButton("Fire1") && Time.time > nextFire && canFire == true){
roundsFired++;
//Instantiate bullet and add force here
}
if (roundsFired >= roundsPerBurst){
canFire = false;
weaponCooldown();
}
}
function weaponCooldown() {
yield WaitForSeconds (CooldownTime);
roundsFired = 0;
canFire = true;
}
also where do you count your nextFire time ? because Time.time gives you the time from your app start till this frame, so at a certain point this will be true till you close your game. maybe this is a problem also
I am fairly sure the issue is with the roundsFired number is. When the roundsFired actually reaches three then the canFire does what it should. From what I have seen the issue is that the roundsFired does not increase when I hold down the trigger which is strange since the rounds to instantiate. That is I think the issue that needs to be solved however I will but in some debug logs to see what happens, thanks.
Ah, I accidentally included that in my Instantiate part, nextFire time is calculated using Time.time + rateOfFire.