Automatic weapon firing too fast when quickly tap firing

godot 4

i have 2 automatic weapons in my fps game, and when you tap fire it quickly instead of holding down fire, it fires way to quickly, how do i fix?

machine gun code:

extends Node3D

@onready var Weapon_Sprite = $CanvasLayer/Control/WeaponSprite

@onready var Hitscan = $Hitscan.get_children()

var can_fire = true

func _ready():
	Weapon_Sprite.play("Idle")
	
func check_hit():
	for ray in Hitscan:
		if ray.is_colliding():
			if ray.get_collider().is_in_group("Enemy"):
				ray.get_collider().take_damage(5)
	
func _process(delta):
	if Input.is_action_pressed("main_fire_weapon") and can_fire:
		Fire()
		await Weapon_Sprite.animation_finished
		can_fire = true
		#Weapon_Sprite.play("Idle")
	if !Input.is_action_pressed("main_fire_weapon") and Weapon_Sprite.animation_finished:
		Weapon_Sprite.play("Idle")
		can_fire = true

func Fire():
	check_hit()
	can_fire = false
	$Fire.play()
	Weapon_Sprite.play("Fire")

chaingun code:

extends Node3D

@onready var Weapon_Sprite = $CanvasLayer/Control/WeaponSprite

@onready var Hitscan = $Hitscan.get_children()

var can_fire = true

func _ready():
	Weapon_Sprite.play("Idle")
	
func check_hit():
	for ray in Hitscan:
		if ray.is_colliding():
			if ray.get_collider().is_in_group("Enemy"):
				ray.get_collider().take_damage(5)
	
func _process(delta):
	if Input.is_action_pressed("main_fire_weapon") and can_fire:
		Fire()
		await Weapon_Sprite.animation_finished
		can_fire = true
		#Weapon_Sprite.play("Idle")
	if !Input.is_action_pressed("main_fire_weapon") and Weapon_Sprite.animation_finished:
		Weapon_Sprite.play("Idle")
		can_fire = true



func _on_weapon_sprite_frame_changed():
	if Weapon_Sprite.animation == ("Fire"):
		if Weapon_Sprite.frame == 1:
			Fire()
			
func Fire():
	check_hit()
	can_fire = false
	$Fire.play()
	Weapon_Sprite.play("Fire")
	

you could add a timer node and not let the player shoot until the timer is over?

already tried that before i decided to use animation signals, it worked out horribly for me

I added an shot_pause function in my game to stop the player spamming the fire button. The fire button does not work when shot_pause is true.

func shoot():
	shot_pause = true
	await get_tree().create_timer(0.5).timeout 
	shot_pause = false

i already have something like that, with it specifically being the can_fire var in my code

1 Like

try if the animation on the gun is playing then it can’t shoot until the animation is over

well it kinda already does that, it calls can_fire = false when it fires and it calls can_fire = true when animation is over, and the chaingun also calls can_fire = true when in mid animation