Making a cooldown system in Unity
My goal this article is to code in a cool down period to my lasers, to keep them from firing all the time.
I’m going to implement it into the Player code, as it will be adding a delay to the instantiation. I’ll be creating a new serialized variable first of all, adding private float _fireRate = 0.2f to the public class, as well as private float _canFire = -1f.
Next I’ll be looking at using Time.time to measure how much time has passed in order to track the fire delay, this gives me the following.
This updates the time the game has been running, adds the number for the fire rate to that, then checks that total against the time the program has spent running, just adding .2 seconds to the current time every time the Player fires.
Finally, I’m going to clean up the code again, just to keep everything neat within my scripts.
That’s it for now! Next time I’ll be working on adding enemies into the game.