- Home /
How to make a variable increase at a constant rate?
I'm making a shoot timer so that I can't shoot too quickly. I have it check to make sure the 'canshoot' variable, which is initialized to 0, equals at least 10 before allowing you to shoot, and then after shooting, sets it back to 0. How can I make 'canshoot' steadily increase?
The shooting aspect of the code looks like this:
function Update () {
var shoot : int = 0;
// Shooting
if (Input.GetKeyDown("space")){
if(shoot >= 10){
shoot = 0;
var instance : GameObject = Instantiate(Laser, transform.position, transform.rotation);
}
}
}
I'd think this would be really easy, just some sort of loop containing canshoot++. I only started learning Java 2 days ago.
Answer by greatwhiteshark17283 · Feb 24, 2014 at 11:28 PM
First, put this somewhere outside of all the functions:
InvokeRepeating("addShoot", 2, 2);
Change the 2s to how many seconds you want canShoot to increase. Then add this function:
function addShoot(){
if(shoot < 10){
shoot++;
}
}
Thanks bro, it worked great. Took too long though, I wasn't satisfied until I brought the parameters down to 0.1.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134623im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Script access (without script name) 1 Answer
Variables Not Updating? 1 Answer
Script is setting variables but they're being reset 1 Answer
variables won show up 1 Answer