Shooting cooldown

Godot Version

v3.6.beta3.mono.official

Question

I tried giving my player the ability to shoot but the player can keep on shooting without any cooldown. i tried connecting a timer to the player but it still isn’t working.

#player script
func _unhandled_input(event: InputEvent) -> void:
	if event.is_action_pressed("ui_accept"):
		spawner.spawn()
		spawner.throw()

#Bullet Spawner
export var fireball_spell_scene: PackedScene = preload("res://Assets/Scences/items/bullets/FireBall/FireBallSpell.tscn")
export var explosion_container_path: NodePath
export var throw_speed := 1000.0

var _fireball_reference: WeakRef

onready var explosion_container := get_node(explosion_container_path)


func spawn() -> void:
	 
	var fireball : RigidBody2D
	fireball = fireball_spell_scene.instance()

	add_child(fireball)
	fireball.explosion_container = explosion_container
	fireball.global_position = global_position
	_fireball_reference = weakref(fireball)

func throw() -> void:
	if not _fireball_reference.get_ref():
		return
	_fireball_reference.get_ref().set_as_toplevel(true)
	_fireball_reference.get_ref().global_position = global_position
	_fireball_reference.get_ref().linear_velocity.x = throw_speed

I usually just use my own timer.

var max_time = 1.0
var time_left = 0.0
var timer_finished = false

func _process(delta):
	if time_left > 0.0:
		time_left -= delta
	elif time_left <= 0.0 and timer_finished == false:
		timer_finished = true

func reset_time_delay():
	time_left = max_time
	timer_finished = false

You can just lock out your fireball by putting timer_finished and reset_time_delay func

if event.is_action_pressed("ui_accept") and timer_finished == true:
	spawner.spawn()
	spawner.throw()
	reset_time_delay()

Edit: corrected my code, there may be other errors that I’m missing.

1 Like

Thanks a lot! my player can now shoot perfectly :grin:

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