Flip sprite for only one animation

So I’ve been working on making my player sprite play the animation that faces the mouse position. I’ve been able to do so. However I don’t have an attack animation for the left attack. I was thinking of something like flipping the sprite for only that animation and flip all the frames of the animtion. However I don’t know how to do so.

player.gd

extends CharacterBody2D


var attacking: bool = false


func _ready():
	%AnimationPlayer.animation_finished.connect(_on_animation_finished)


func _physics_process(delta):
	var direction = Input.get_vector("move_left", "move_right", "move_up", "move_down")
	if attacking:
		velocity = Vector2.ZERO
	else:
		velocity = direction * 400
	move_and_slide()
	
	if not attacking:
		if velocity.length() > 0.0:
			%Bluwee.play_run()
		else:
			%Bluwee.play_idle()
	
	if Input.is_action_just_pressed("attack") and not attacking:
		attacking = true
		handle_attack_animation()

func handle_attack_animation():
	var mouse_position = get_global_mouse_position()
	var vector_to_mouse = (mouse_position - global_position).normalized()
	var angle_to_mouse = vector_to_mouse.angle()  # Angle in radians
	# Convert angle to degrees for easier comparisons
	var angle_in_degrees = rad_to_deg(angle_to_mouse)
	
	# Adjust for top-down orientation (0 degrees = up)
	if angle_in_degrees < 0:
		angle_in_degrees += 360
	
	# Determine animation based on the angle
	if angle_in_degrees >= 315 or angle_in_degrees < 45:
		%Bluwee.play_attack()  # Mouse is above the player
	elif angle_in_degrees >= 45 and angle_in_degrees < 135:
		%Bluwee.play_down_attack()  # Mouse is to the right of the player
	elif angle_in_degrees >= 225 and angle_in_degrees < 315:
		%Bluwee.play_up_attack()  # Mouse is below the player
	elif angle_in_degrees >= 135 and angle_in_degrees < 225:
		%Bluwee.play_attack()  # Placeholder for left attacks if needed

func _on_animation_finished(animation: String):
	# Reset attacking flag when the attack animation is done
	if animation in ["right_attack", "back_attack", "front_attack"]:
		attacking = false
		%Bluwee.play_idle()

Node2D.gd

extends Node2D


func play_idle():
	%AnimationPlayer.play("idle")

func stop_idle():
	%AnimationPlayer.stop("idle")

func play_run():
	%AnimationPlayer.play("run")

func play_attack():
	%AnimationPlayer.play("right_attack")

func play_up_attack():
	%AnimationPlayer.play("back_attack")

func play_down_attack():
	%AnimationPlayer.play("front_attack")

Set the sprite scale.x to -1, that will flip the image, when the attack finishes, set to 1 again

I just tried to do it:

func play_left_attack():
	%Bluwee.scale.x = -1
	%AnimationPlayer.play("left_attack")
	%Bluwee.scale.x = 1

However, the player still playes the animation for right_attack and after finishing it, it just stays stuck on the last frame and the game freezes.

Probably the game freezes because you don’t set attacking to false after the animation finishes, you don’t check for the left_attack in _on_animation_finished. Try just play the right_attack with the scale.x = -1 and see what happens

I was able to fix it thank you!

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