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

Rate of fire

Discussion in 'Scripting' started by TheDiddyHop, Jun 2, 2014.

  1. TheDiddyHop

    TheDiddyHop

    Joined:
    Jul 26, 2013
    Posts:
    30
    I'm working on a script for guns in my game, and i'd like to customize the rate of fire. This script is for automatic weapons. How would i go about doing this?
    Code (csharp):
    1. var Bullet : Transform;
    2. var Spawn : Transform;
    3. var shellSpawn : Transform;
    4. var shell : Transform;
    5. var muzzleflash : Transform;
    6. var BulletForce = 4000 ;
    7. var shellForce = 100 ;
    8. var Sounds:AudioClip[];
    9. var RateOfFire = 0 ;
    10.  
    11. function Update ()
    12. {
    13.     if(Input.GetButton("Fire1"))
    14.     {
    15.        Shot();
    16.     }
    17. }
    18.  
    19. function Shot()
    20. {
    21.     var pel = Instantiate(Bullet, Spawn.position, Spawn.rotation);
    22.     pel.rigidbody.AddForce(Spawn.forward * BulletForce);
    23.     var sel = Instantiate(shell, shellSpawn.position, shellSpawn.rotation);
    24.     sel.rigidbody.AddForce(shellSpawn.forward * shellForce);
    25.     Instantiate(muzzleflash, Spawn.position, Spawn.rotation);
    26.     audio.clip = Sounds[Random.Range(0, Sounds.length)];
    27.     audio.Play();
    28. }
     
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    This should work :

    Code (csharp):
    1.  
    2. var fireRate : float = 0.5;
    3. var nextFire : float = 0;
    4.  
    5. function Update(){
    6. if(Input.GetButton("Fire1")  Time.time > nextFire){
    7. nextFire = Time.time + fireRate;
    8. shot();
    9. }
    10. }
    11.  
    12.  
     
  3. StaticNova

    StaticNova

    Joined:
    Feb 23, 2013
    Posts:
    60
    Intense_Gamer94's solution works but is not frame rate independent. You can achieve that by using Time.deltaTime.

    For example:

    Code (csharp):
    1.  
    2. var Bullet : Transform;
    3. var Spawn : Transform;
    4. var shellSpawn : Transform;
    5. var shell : Transform;
    6. var muzzleflash : Transform;
    7. var BulletForce = 4000 ;
    8. var shellForce = 100 ;
    9. var Sounds:AudioClip[];
    10. var RateOfFire = 0 ;
    11.  
    12. var fireRate: float = 0.5;
    13. var fireTimer: float = 0.0;
    14. var canFire: boolean = true;
    15.  
    16. function Update ()
    17. {
    18.     if(Input.GetButton("Fire1")  canFire) {
    19.         canFire = false;
    20.         Shot();
    21.     }
    22. }
    23.  
    24. function Shot()
    25. {
    26.     var pel = Instantiate(Bullet, Spawn.position, Spawn.rotation);
    27.     pel.rigidbody.AddForce(Spawn.forward * BulletForce);
    28.     var sel = Instantiate(shell, shellSpawn.position, shellSpawn.rotation);
    29.     sel.rigidbody.AddForce(shellSpawn.forward * shellForce);
    30.     Instantiate(muzzleflash, Spawn.position, Spawn.rotation);
    31.     audio.clip = Sounds[Random.Range(0, Sounds.length)];
    32.     audio.Play();
    33.  
    34.     StartCoroutine("ShotTimer");
    35. }
    36.  
    37. function ShotTimer() {
    38.    
    39.     while(canFire == false) {
    40.         fireTimer += Time.deltaTime;
    41.         if(fireTimer > fireRate) {
    42.             fireTimer = 0.0;
    43.             canFire = true;
    44.             break;
    45.         }
    46.         yield;
    47.     }
    48. }
    49.  
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    It doesnt matter if its frame rate independent in this situation, as the fire rate is inferred by the user clicks.

    intense_gamer offered a better solution.