Weapon cooldown

Godot Version

4.3

Question

how can I add a cool down to my pistol

I have a pistol like weapon, and i want to add a cool down, with the current await create timer thing i have, it seems to not work.

	if Input.is_action_just_released("shoot"):
		shoot()
		await get_tree().create_timer(0.5).timeout

Yall have any ideas?

this is what I did for my game:

I declared a variable called last_shot and initialized it to the value returned by Time.get_ticks_msec()

then, I had a constant variable that specified the cooldown time, in milliseconds (for example, 500 for half a second)

finally, when the player wants to shoot, you get the difference between last_shot and the value returned by Time.get_ticks_msec() in that specific moment. If it is bigger than the cooldown time, the player can shoot and last_shot is updated to Time.get_ticks_msec(). Otherwise, do not shoot and leave last_shot unchanged

hope it helps!

I generally have a Timer called GunCooldown already in the scene with the Wait time already set, and a variable like can_shoot : bool = true at the top of my script. Where I deal with input has

	if Input.is_action_just_pressed("shoot") and can_shoot:
		shoot()

And then the shoot function

func shoot() -> void:

	can_shoot = false
	$GunCooldown.start()
	
	# spawn the bullet
	var b = bullet_scene.instantiate()
	get_tree().root.add_child(b)
	
	$ShootSound.play()

Check out my tutorial where I use a fire cadence and cooldown.

I used an ammo variable

if Input.is_action_just_released("shoot"):
		if ammo != 0
		ammo = 0
		shoot()
		await get_tree().create_timer(0.5).timeout
		ammo = 1

to anyone reading this in the future, just remember to reset the can_shoot variable with a timeout() signal

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.