My timer has inconsistent timings

Godot Engine v4.3.stable.official.77dcf97d8

I am creating an RPG with combat mechanics and am trying to make a weapon with a cool-down. The trouble is that the gun can shoot more often than should be possible. When I press the left mouse button which is mapped to the shoot mechanic in the project’s input map, I can get two bullets to spawn in close succession and thereafter the gun shoots at regular intervals.

I also have a global autoload timer that acts as the delay timer for the gun.
The code for that is at the bottom

here’s the code:

extends Node2D

@onready var marker_2d: Marker2D = $Marker2D

@onready var prelim_bullet_scene = preload("res://Items/Test Items/Prelim Bullet/Prelim Bullet.tscn")


func _ready() -> void:
	GlobalWeaponTimer.timer.wait_time = 0.5


func _process(_delta: float) -> void:
	
	look_at(get_global_mouse_position())
	
	if Input.is_action_pressed("Attack") and GlobalWeaponTimer.can_shoot == true:
		GlobalWeaponTimer.can_shoot = false
		shoot()

func shoot():
	var prelim_bullet = prelim_bullet_scene.instantiate()
	get_parent().get_parent().get_parent().add_child(prelim_bullet)
	prelim_bullet.global_position = marker_2d.global_position
extends Node2D

@onready var timer: Timer = $Timer

var can_shoot = true


func _on_timer_timeout() -> void:
	can_shoot = true
	print("Timed")

Try to use is_action_just_pressed():

if Input.is_action_just_pressed("Attack") and GlobalWeaponTimer.can_shoot:

I have tried that and it does work.
The trouble is that I want the player to be able to hold down and shoot and not have to click every time.

Where do you start the timer?

I autostart it in the global singleton GlobalWeaponTimer
image

this is the global autoload in the settings menu

But you should only start it when you shoot. This is probably the reason you sometimes can shoot two bullets

Lemme try that

Absolute Godsend
It worked.
Thanks so much.
Have a great day

1 Like

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