Put a Leash on that Pistol by Creating A Cooldown System in Unity

In my last article, the player got the ability to shoot bullets through instantiating prefabs. Every time the user presses the space key, a bullet will fire, no matter how fast they are pressing. In this article I will go over how to put a governor on …


This content originally appeared on Level Up Coding - Medium and was authored by Jared Amlin

In my last article, the player got the ability to shoot bullets through instantiating prefabs. Every time the user presses the space key, a bullet will fire, no matter how fast they are pressing. In this article I will go over how to put a governor on the fire ability.

The firing mechanism can have a timer delay that basically says, “you can only fire every X seconds.” In order to use that timer, I need to use real time, which can be done in Unity with Time.time.

Time is considered a float, probably because it elapses in tiny increments like fractions of a second.

A few important things to take away from this next snippet, are that the time represented, is the time that has elapsed since the game has started. If the game has been running for 8 seconds, you have a Time.time variable of 8. The longer the game runs, the larger that variable becomes.

Unity gives us an example for using Time.time, and it just so happens to be on how to implement a cool down system on a firing mechanism. It doesn’t get any easier than that!

In order to incorporate this premium example into my game, I will first need a few variables. I set the fire rate variable to 0.5f as a default, which will let the player fire a bullet no more than every half a second. I then serialize the field so I can adjust that value later from the inspector. I also need another variable for my can fire, which I set to zero, for reasons I will get to shortly.

In my update method where I am checking the space key for input, I use the && operator to check two conditions. If the space key is being held down and also (&&) Time.time (elapsed time since the game started) is greater than the can fire variable (zero), the bullet will fire.

Here is where it all comes together. Being that the game has been running for longer than zero seconds (Time.time > _canFireBullet), the player can fire a single bullet. After that in the fire bullet method, the can fire variable gets a new value by adding Time.time to our fire rate. Now the value of can fire is our Time.time value plus 0.5 from the fire rate. That means that Time.time is not greater than the new can fire value, so the player can’t fire. Time.time keeps updating, so after a half a second (fire rate), the Time.time value will once again be larger than the can fire, and the player can fire again!


Put a Leash on that Pistol by Creating A Cooldown System in Unity was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Jared Amlin


Print Share Comment Cite Upload Translate Updates
APA

Jared Amlin | Sciencx (2021-05-07T22:12:24+00:00) Put a Leash on that Pistol by Creating A Cooldown System in Unity. Retrieved from https://www.scien.cx/2021/05/07/put-a-leash-on-that-pistol-by-creating-a-cooldown-system-in-unity/

MLA
" » Put a Leash on that Pistol by Creating A Cooldown System in Unity." Jared Amlin | Sciencx - Friday May 7, 2021, https://www.scien.cx/2021/05/07/put-a-leash-on-that-pistol-by-creating-a-cooldown-system-in-unity/
HARVARD
Jared Amlin | Sciencx Friday May 7, 2021 » Put a Leash on that Pistol by Creating A Cooldown System in Unity., viewed ,<https://www.scien.cx/2021/05/07/put-a-leash-on-that-pistol-by-creating-a-cooldown-system-in-unity/>
VANCOUVER
Jared Amlin | Sciencx - » Put a Leash on that Pistol by Creating A Cooldown System in Unity. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/07/put-a-leash-on-that-pistol-by-creating-a-cooldown-system-in-unity/
CHICAGO
" » Put a Leash on that Pistol by Creating A Cooldown System in Unity." Jared Amlin | Sciencx - Accessed . https://www.scien.cx/2021/05/07/put-a-leash-on-that-pistol-by-creating-a-cooldown-system-in-unity/
IEEE
" » Put a Leash on that Pistol by Creating A Cooldown System in Unity." Jared Amlin | Sciencx [Online]. Available: https://www.scien.cx/2021/05/07/put-a-leash-on-that-pistol-by-creating-a-cooldown-system-in-unity/. [Accessed: ]
rf:citation
» Put a Leash on that Pistol by Creating A Cooldown System in Unity | Jared Amlin | Sciencx | https://www.scien.cx/2021/05/07/put-a-leash-on-that-pistol-by-creating-a-cooldown-system-in-unity/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.