Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Burst Fire gun script having issues counting rounds

Discussion in 'Scripting' started by Zac-Yeates, Oct 5, 2015.

  1. Zac-Yeates

    Zac-Yeates

    Joined:
    Jan 23, 2015
    Posts:
    20
    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
    Code (JavaScript):
    1. function Update () {
    2.    if (Input.GetButton("Fire1") && Time.time > nextFire && canFire == true){
    3.       roundsFired++;
    4.       //Instantiate bullet and add force here
    5.    }
    6.  
    7.    if (roundsFired >= roundsPerBurst){
    8.       canFire = false;
    9.       weaponCooldown();
    10.    }
    11.    else{
    12.       canFire = true;
    13.    }
    14. }
    15.  
    16. function weaponCooldown() {
    17.    yield WaitForSeconds (CooldownTime);
    18.    roundsFired = 0;
    19. }
    Any help is appreciated.

    Zac Yeates
     
  2. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    Have you tried to set up some debug logs to see what roundsfired , canfire states you have ?

    also what i would change is
    Code (JavaScript):
    1. function Update () {
    2.    if (Input.GetButton("Fire1") && Time.time > nextFire && canFire == true){
    3.       roundsFired++;
    4.       //Instantiate bullet and add force here
    5.    }
    6.    if (roundsFired >= roundsPerBurst){
    7.       canFire = false;
    8.       weaponCooldown();
    9.    }
    10. }
    11. function weaponCooldown() {
    12.    yield WaitForSeconds (CooldownTime);
    13.    roundsFired = 0;
    14.    canFire = true;
    15. }
     
  3. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    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
     
  4. Zac-Yeates

    Zac-Yeates

    Joined:
    Jan 23, 2015
    Posts:
    20
    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.
     
  5. Zac-Yeates

    Zac-Yeates

    Joined:
    Jan 23, 2015
    Posts:
    20
    Ah, I accidentally included that in my Instantiate part, nextFire time is calculated using Time.time + rateOfFire.