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. Dismiss Notice

Question How to add firerate ?

Discussion in 'Scripting' started by Danielddw, Jun 8, 2023.

  1. Danielddw

    Danielddw

    Joined:
    Dec 22, 2022
    Posts:
    8
    Im working on a game in wich the player shoots different projectiles but I dont want them to be spawned as fast and every time the player press a key, I want to have a pause between every shoot.

    Here is the code:


    {
    public GameObject player;
    public GameObject[] projectilePrefabs;
    public bool CanShoot = true;
    public float FireRate = 0.5f;
    public float NextShoot = -1.0f;

    private void Update()
    {
    if(Time.time > NextShoot)
    {
    CanShoot = true;
    }

    if(Input.GetKeyDown(KeyCode.UpArrow) && CanShoot == true ) // we have user input and Canshoot is true
    {
    Instantiate(projectilePrefabs[0], transform.position, transform.rotation); // we shoot
    NextShoot = Time.time + FireRate; // reset fire
    }
    if(Input.GetKeyDown(KeyCode.DownArrow) && CanShoot == true )
    {
    Instantiate(projectilePrefabs[1], transform.position, transform.rotation);
    NextShoot = Time.time + FireRate;
    }
    if(Input.GetKeyDown(KeyCode.LeftArrow) && CanShoot == true)
    {
    Instantiate(projectilePrefabs[2], transform.position, transform.rotation);
    }
    if (Input.GetKeyDown(KeyCode.RightArrow) && CanShoot == true)
    {
    Instantiate(projectilePrefabs[3], transform.position, transform.rotation);
    NextShoot = Time.time + FireRate;
    }
    if (Input.GetKeyDown(KeyCode.Space) && CanShoot == true)
    {
    Instantiate(projectilePrefabs[4], transform.position, transform.rotation);
    NextShoot = Time.time + FireRate;
    }
    if (Input.GetKeyDown(KeyCode.X) && CanShoot == true)
    {
    Instantiate(projectilePrefabs[5], transform.position, transform.rotation);
    NextShoot = Time.time + FireRate;
    }
    }
    }
     
  2. whoft

    whoft

    Joined:
    Jul 1, 2021
    Posts:
    17
    It would be great if you could use code tags, makes your post a whole lot more readable :)

    One idea would be to add a timer that counts down after every shot, only allowing you to fire once the timer has reached zero again.

    Code (CSharp):
    1. {
    2. public GameObject player;
    3. public GameObject[] projectilePrefabs;
    4. public bool CanShoot = true;
    5. public float FireRate = 0.5f;
    6. public float fireTimer;
    7.  
    8. private void Update()
    9. {
    10. fireTimer -= Time.deltaTime;
    11.  
    12. if(Input.GetKeyDown(KeyCode.UpArrow) && CanShoot == true && fireTimer <= 0f) // we have user input and Canshoot is true
    13. {
    14. Instantiate(projectilePrefabs[0], transform.position, transform.rotation); // we shoot
    15. fireTimer = FireRate;
    16. }
    17. if(Input.GetKeyDown(KeyCode.DownArrow) && CanShoot == true && fireTimer <= 0f)
    18. {
    19. Instantiate(projectilePrefabs[1], transform.position, transform.rotation);
    20. fireTimer = FireRate;
    21. }
    22. if(Input.GetKeyDown(KeyCode.LeftArrow) && CanShoot == true && fireTimer <= 0f)
    23. {
    24. Instantiate(projectilePrefabs[2], transform.position, transform.rotation);
    25. fireTimer = FireRate;
    26. }
    27. if (Input.GetKeyDown(KeyCode.RightArrow) && CanShoot == true && fireTimer <= 0f)
    28. {
    29. Instantiate(projectilePrefabs[3], transform.position, transform.rotation);
    30. fireTimer = FireRate;
    31. }
    32. if (Input.GetKeyDown(KeyCode.Space) && CanShoot == true && fireTimer <= 0f)
    33. {
    34. Instantiate(projectilePrefabs[4], transform.position, transform.rotation);
    35. fireTimer = FireRate;
    36. }
    37. if (Input.GetKeyDown(KeyCode.X) && CanShoot == true && fireTimer <= 0f)
    38. {
    39. Instantiate(projectilePrefabs[5], transform.position, transform.rotation);
    40. fireTimer = FireRate;
    41. }
    42. }
    43. }
    What this does is it creates a timer such that you can only shoot when the timer has run out. This timer is reset after every shot, so there's a delay of length FireRate (in seconds) before your next shot.

    Also, I removed the NextShoot because that seemed to be trying to do something similar in a nonfunctional fashion, feel free to re-add it if it's important to something else.
     
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,588
    Firerate is effectively just a cooldown. There is two main ways to implement cooldowns. You can remember the last time of activation and only allow the next activation once some time period has passed. Or you can set some timer variable to a certain value and subtract from it each frame by Time.deltaTime to track the remaining cooldown time directly.

    Also, please use code tags.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Yoreki likes this.