Double tap not working as intended

Godot Version

godot 4

Question

I’m working on a melee mechanic for my game where tapping a a key once will punch but double tapping will throw a block. for some reason this code isn’t working for this purpose. anyone see any reason why?

extends Node3D

@export var DOUBLETAP_DELAY = .25
var doubletap_time = .25
var last_event = "null"

@export var rightAnim : AnimationPlayer


func _process(delta):
	doubletap_time -= delta
	_right_hand()
	
	

func _right_hand():
	if Input.is_action_just_pressed("RightHand"):
		if last_event == "RightHand" && doubletap_time >= 0 : 
			rightAnim.play("Block")
			print("block")
			last_event = "null"
		elif doubletap_time < 0:
			rightAnim.play("punch")
			last_event = "Right"
		else:
			last_event = "Right"
		doubletap_time = DOUBLETAP_DELAY

Is it because you are checking if the last_event is RightHand but you only set it to Right in the other blocks?

1 Like

I think that was the issue probably, yeah. I’ve got it working reasonably well now, timing just needs some tweaking

extends Node3D

@export var DOUBLETAP_DELAY = .25
var doubletap_time = .25
var last_event_r = "null"
var last_event_l = "null"

var rightPunched = true
var leftPunched = true

@export var rightAnim : AnimationPlayer
@export var leftAnim : AnimationPlayer

func _process(delta):
	doubletap_time -= delta
	_right_hand()
	_left_hand()
	

func _right_hand():
	if Input.is_action_just_pressed("RightHand"):
		rightPunched = false
		if last_event_r == "RightHand" && doubletap_time >= 0 : 
			rightAnim.play("Block")
			print("block")
			last_event_r = "null"
			rightPunched = true
		else:
			last_event_r = "RightHand"
			doubletap_time = DOUBLETAP_DELAY
		
	if doubletap_time < 0 && rightPunched == false:
		rightAnim.play("punch")
		last_event_r = "RightHand"
		rightPunched = true


func _left_hand():
	if Input.is_action_just_pressed("LeftHand"):
		leftPunched = false
		if last_event_l == "LeftHand" && doubletap_time >= 0 : 
			leftAnim.play("Block")
			print("block")
			last_event_l = "null"
			leftPunched = true
		else:
			last_event_l = "LeftHand"
			doubletap_time = DOUBLETAP_DELAY
		
	if doubletap_time < 0 && leftPunched == false:
		leftAnim.play("punch")
		last_event_l = "LeftHand"
		leftPunched = true