Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
02-25-2014, 12:33 AM #1
- Join Date
- Feb 2014
- Thanked 0 Times in 0 Posts
How to increase a variable at a constant rate?
This should be super simple, I'm almost ashamed to ask. I thought just putting variable++ in a loop of some sort might work, but I haven't learned loops yet.
I'm making a game in unity and my ship fires too quickly. I need a timer for the firing mechanism. I already have it set up. First, a variable(canshoot for instance) is initialized to 0. Then(with the help I get from you guys), I have the variable increase steadily. Then I check if space is being pressed, if it is, I check if canshoot is greater than or equal to 10. If canshoot is greater than or equal to 10, it creates the bullet instance which has it's own script, then sets canshoot back to 0 to restart the cycle.
If you have a better idea, feel free to share, but please answer the question because I'm pretty sure it will work fine. I'd show you the code, but I don't think it would make sense to you unless you use Unity too.
02-25-2014, 01:38 AM #2
I'd have a look at setTimeout if I were you - does pretty much what you describe. A simpler way might be with a boolean flag - set canshoot to false, set a 10 second timeout at which point canshoot gets set to true. Then on spacebar event, all you have to check is if(canshoot) and if that returns true, run the bullet script, set canshoot to false and set another 10 second timeout
and so on and so forth.