How to get timer to start

Godot Version

4.2

Question

I have this code where a raycast fires after a timer went off and should start when I press a button but it doesnt start

Heres the code

var fire = Input.is_action_pressed("Fire")

func _process(delta):
	if fire:
		$"../Timer".start()

func _on_timer_timeout():
	shootaudio.play()
	dot.set_position(ray.get_collision_point())
	print (ray.get_collision_point())

I think you want the Input.is_action_pressed check in the if statement in the process function. As it is now you are setting fire equal to the state of the input at the time the variable is declared and never rechecking it.

Change the bottom portion to:
if Input.is_action_pressed(“Fire”):
$“…/Timer”.start()

1 Like

It would be better to check in the _input or _unhandled_input function, since if you press down the button for multiple frames, then the timer will keep starting each from you press it down.

1 Like

This works but i want it to stop after the button is relaesed

If oneshot is false, then the timer will start again when it times out. You can also detect button release in input and unhandled input like this:

if event.is_action_released("Fire"):
    $"../Timer".stop()
3 Likes

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