Detect mouse button release after being held down

I’m trying to make a bow that adds speed to a projectile while the left mouse button is pressed. After the mouse button is released, the projectile gets instantiated through a function.

I have the following code so far:

func _physics_process(delta):

	$Bow.look_at(get_global_mouse_position())
	if Input.is_mouse_button_pressed(1):
		arrow.speed += 5
		if Input.is_action_just_released("Shoot"): #Shoot is a left mouse button input
			shoot()

doing this doesn’t seem to detect the mouse button getting released, though when I replace “Shoot” for another input it works just fine. What am I missing exactly? Any help would be appreciated.

Double check your inputs to see if everything is setup correctly. Else, create a new input as the other inputs seem to work just fine.

Make sure your device is functional (mouse). Try another mouse even.

Send a screenshot of your input map action. Maybe I could spot something.

many thanks, here’s the input map.

Screenshot from 2024-07-08 13-22-21

The mouse seems to be working fine, and using

if Input.is_action_just_released("Shoot"):
			print("test")

to debug the the input itself also worked just ok. The problem is when I try detecting the mouse release inside of a condition that’s checking for a mouse hold. My guess is that the engine can’t handle both of those? If that’s the case, how would I fix it or work around it?

This doesn’t work. Because Input.is_mouse_button_pressed
is different from Input.is_action_just_released and Input.is_action_just_pressed
I don’t remember if there’s a way to decrease the amount of ticks because += 5 may be too much at 5 * 60 per second.

1 Like

Input.is_mouse_button_pressed just “pops” once and stops.

1 Like

I mean sure. International internet semantic aneurysms.
+= 5 may be too much at 5 × 60 per second

Good catch. Try the following code, @Makiatto.

func _physics_process(delta):

	$Bow.look_at(get_global_mouse_position())
	if Input.is_mouse_button_pressed(1):
		arrow.speed += 5 # or whatever bow pull back animation
	if Input.is_action_just_released("Shoot"): #Shoot is a left mouse button input
		shoot()

If you have any questions as to why, I can try to come up with a better explanation.

Yeah if you could clarify why it needs to be outside of the condition that would be appreciated. It works though, thanks!

Also I’m doing +5 just for the time being. I’ll get everything working first and polish it later, +5 is just easier to visualize.

Here’s using elif and the input action for consistency.

func _physics_process(delta):
	$Bow.look_at(get_global_mouse_position())
	if Input.is_action_pressed("Shoot"):
		arrow.speed += 5
	elif Input.is_action_just_released("Shoot"):
		shoot()

Code inside of a if statement will only run if the condition is true, including other if statements. It would check if you released the button only if you also pressed it in the same frame, which is impossible in Godot.

func _physics_process(delta):
	$Bow.look_at(get_global_mouse_position())
	if Input.is_mouse_button_pressed(1): # Button is held down!
		arrow.speed += 5 # add 5 speed!
		# no time has passed, functions run in one frame
		# Button is held down so this cannot be true
		if Input.is_action_just_released("Shoot"): 
			shoot()

There is no single frame where the button is both held and just released.

2 Likes

got it, thanks everyone a lot!

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