How do I make a cooldown on shooting

Get help using Construct 2

Post » Sun Dec 14, 2014 11:30 pm

I want to make it so that there is kind of a cool down on shooting so that you cant just spam the shoot button
B
4
Posts: 14
Reputation: 228

Post » Sun Dec 14, 2014 11:51 pm

Spawn projectiles using two conditions:

'Is FireButton down?'

and

'every x seconds'

then spawn your projectiles.
Don't lose your work. Backup your game with Dropbox.
B
44
S
10
G
10
Posts: 1,106
Reputation: 9,202

Post » Sun Dec 14, 2014 11:59 pm

Thanks!
B
4
Posts: 14
Reputation: 228

Post » Mon Dec 15, 2014 2:14 am

the problem with using every X seconds is that you wont shoot right away when you click sometimes, which feel weird especially for longer delays (imagine holding down the fire key for 6 seconds then the bullet comes out).

a better way to do it is to have a timer variable.

every tick
: add to variable object.shootTimer -> this.dt

// this value gives you the time since the last shot fired.

is fire button down
is object.shootTimer > cooldown
: shoot
: set variable object.shootTimer -> 0

// shoot the object, reset the cooldown
B
77
S
13
G
8
Posts: 1,973
Reputation: 9,891

Post » Mon Dec 15, 2014 2:25 am

Might be better to have the action of firing set a variable, then every tick subtract from that until it reaches zero. That way you'll always fire the first time you press the button.
B
5
Posts: 17
Reputation: 259

Post » Mon Dec 15, 2014 3:51 am

honestly it worked just fine since i just wanted to make it be able to shoot every .5 seconds
B
4
Posts: 14
Reputation: 228

Post » Mon Dec 15, 2014 4:01 am

QuaziGNRLnose wrote:the problem with using every X seconds is that you wont shoot right away when you click sometimes, which feel weird especially for longer delays (imagine holding down the fire key for 6 seconds then the bullet comes out).


Isn't that what they want? i.e. not to be able to fire at will. If you can always shoot right away, how is that a cooldown? Having said that, I usually use a Timer also. Or the Cooldown plugin.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Mon Dec 15, 2014 4:53 am

@codah, It only shoots right away if greater than cooldown time has passed, you wont shoot right away if you shot 0 seconds ago, you'll have to wait the length of the cooldown, but if you havent shot for 10 seconds, you wouldn't expect to wait possible the entire duration of a cooldown for your first shot.

imagine you had a gun that could only shoot every minute, if you used every "60" seconds, then if the timer was at 61 seconds, and you clicked, you wouldn't shoot a bullet for a whole minute, and would have to be holding the key down for 59 seconds until you shoot, even if you haven't shot once. by using a variable, if your weapon is "cooled down" for the required length of time, then you don't have to arbitrarily hold down the key till the timer hits some value, you'll fire, and then youll have to wait, and youll fire again, etc. Reloading takes a set amount of time after firing your first shot in a series of shots, it doesn't make you wait a random amount of time based on the game timer before being able to fire.
B
77
S
13
G
8
Posts: 1,973
Reputation: 9,891

Post » Mon Dec 15, 2014 6:12 am

QuaziGNRLnose wrote:@codah, It only shoots right away if greater than cooldown time has passed, you wont shoot right away if you shot 0 seconds ago, you'll have to wait the length of the cooldown, but if you havent shot for 10 seconds, you wouldn't expect to wait possible the entire duration of a cooldown for your first shot.

imagine you had a gun that could only shoot every minute, if you used every "60" seconds, then if the timer was at 61 seconds, and you clicked, you wouldn't shoot a bullet for a whole minute, and would have to be holding the key down for 59 seconds until you shoot, even if you haven't shot once. by using a variable, if your weapon is "cooled down" for the required length of time, then you don't have to arbitrarily hold down the key till the timer hits some value, you'll fire, and then youll have to wait, and youll fire again, etc. Reloading takes a set amount of time after firing your first shot in a series of shots, it doesn't make you wait a random amount of time based on the game timer before being able to fire.


It's funny because I was going to write a post just like yours, then second guessed myself. I misinterpreted the way the Every x seconds worked in conjunction with something like Mouse: Left button down, or whatever. I guess I always user Timers anyway so I don't have this issue.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596


Return to How do I....?

Who is online

Users browsing this forum: darkrealos, luckyrawatlucky and 20 guests