Jabbing animation

Godot Version

4.3.official.stable

Question

Hi team!!!

So i am trying to play the “Jab” animation whenever I press the J key (the one I use for jabbing) the character just shakes like if he is going to jab. I know my code is not the cleanest lol! KingGD the best is helping me on cleaning it out, but for now here is a video of what is happening and also the code.

Thanks,


#Variable for the Player Walking Speed.
@export var speed = 300

#Variable for the AnimatedSprite to use for flip etc.
@onready var adam_animated_sprite = $AnimatedSprite2D

#Jabbing variable.
@export var jabbing = false
		
#This code controls the up, down, right and left of the Character.
func _process(delta: float) -> void:

	var direction = Input.get_vector("ui_left","ui_right","ui_up","ui_down")
	velocity = direction * speed
	move_and_slide()
	
# This is and if that calls a function below for jabbing
	if Input.is_action_just_pressed("Jab"):
		jab()
		
#This code is to Flip the Character and playes the animation for walking, Jabbing.
func _physics_process(delta: float) -> void:
	
	if Input.is_action_pressed("ui_left"):
		adam_animated_sprite.flip_h = true
		adam_animated_sprite.play("Walking")	

	elif Input.is_action_pressed("ui_right"):
		adam_animated_sprite.flip_h = false
		adam_animated_sprite.play("Walking")
		
	elif Input.is_action_pressed("ui_up"):
		adam_animated_sprite.play("Walking")
		
	elif Input.is_action_pressed("ui_down"):
		adam_animated_sprite.play("Walking")
		
	else:
		adam_animated_sprite.play("Idle")
		
func jab():
	jabbing = true
	adam_animated_sprite.play("Jab")```



Here is the video!
![Recording 2024-11-10 131801|video](upload://7vwFYr78466dLBfsxfAc4kLMdnH.mp4)

You need to remove your idle animation that may be triggered during the _physics_process routine.

1 Like

@KingGD got thru he helped clean my code and help with the jabbing issue! now onto why is jabbing slow! the jab is a start of a combo, but your solution was correct!!!

#Variable for the Player Walking Speed.
@export var speed := 300

#Variable for the AnimatedSprite to use for flip etc.
@onready var adam_animated_sprite = $AnimatedSprite2D

#Jabbing variable.
@export var jabbing := false
		
#This code controls the up, down, right and left of the Character.

func _process(delta: float) -> void:
	var direction = Input.get_vector("ui_left","ui_right","ui_up","ui_down")
	velocity = direction * speed
	move_and_slide()
	
	if Input.is_action_just_pressed("Jab"):
		jab()

	play_animations(direction)

	if direction.x: adam_animated_sprite.flip_h = true if direction.x < 0 else false
		
func play_animations(dir):
	if jabbing:
		adam_animated_sprite.play("Jab")
	else:
		if dir:
			adam_animated_sprite.play("Walking")
		else:
			adam_animated_sprite.play("Idle")

func jab():
	jabbing = true
	await get_tree().create_timer(0.8).timeout #Attack Duration
	jabbing = false```

You are waiting 0.8 seconds for the jab animation to complete.

You should use the signal animation finished instead of timer.

1 Like

Thank you you are right about the timer! I am a noob on gdscripting (havent code in a lot of years) searching signal animation finished!!! You all are geniuses!!!

Appreciate you all!!!

Sorry to ask but how would I change this to be the signal animation finished? researching but I see the animation_finished has a function. Would I only need to change the timer part or the function to be _animation_finished

Thanks,

Got it working!!! Thanks!!! now want to make him not slide or move while “attacking” like on this video.

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