Godot Version
4.3
Question
so basically im trying to make the player able to attack even when running and i cant get it to work ```extends CharacterBody2D
var speed = 200
func _process(delta):
var input_vector = Vector2.ZERO
if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
$AnimatedSprite2D.play("hitsword")
if Input.is_action_pressed("ui_right"):
input_vector.x += 1
$AnimatedSprite2D.flip_h = false
$AnimatedSprite2D.play("run")
elif Input.is_action_pressed("ui_left"):
input_vector.x -= 1
$AnimatedSprite2D.flip_h = true
$AnimatedSprite2D.play("run")
elif Input.is_action_pressed("ui_up"):
input_vector.y -= 1
$AnimatedSprite2D.play("run")
elif Input.is_action_pressed("ui_down"):
input_vector.y += 1
$AnimatedSprite2D.play("run")
else:
$AnimatedSprite2D.play("idle")
input_vector = input_vector.normalized()
velocity = input_vector * speed
move_and_slide()
An animation player can only play one animation at a time. And it looks like you are checking input every frame. So if your running, hit attack, the attack will start but immediately turn back to run in the same frame.
You should use an animation tree with a state machine. That once it detects an attack you can set it up to play the full animation before returning to whatever movement state the player is in.
An animation tree will also let you blend animations, so you have have the lower half of your character running (for example) while the upper half of your character is attacking.
1 Like
Can’t play two animations with an AnimatedSprite2D. An AnimationTree can blend certain animations, though with 2D Sprites it’s rather impossible to blend animations of whole images. How do you intend this to look? How would you display two spritesheet images at the same time?
I am so much in the mindset of 3D that I forgot not all 3D techniques transfer to 2D.
1 Like
What gertkeno said is true, you have to make every cobination animation