Sword rotation and animation, top down 2D

Godot Version

Godot 4.5

Question

Hey, so I have this strange problem with my sword...

a script is attached to sword that uses the lookat() to track the mouse position and look at it, it works fine, the sword scene is then instantiated in the player scene. everything works fine until I swing it:
func swing():
attacking = true
$AnimationPlayer.play(“swing”)
$PivotPoint/Sprite2D/Area2D/WeaponHitBox.disabled = false

when the sword is on the right side of the player (which means mouse is also on the right side), the swing animation plays fine, however when it’s on the left side instead of swinging downward it swings upward

I understand the problem is with the rotation of the sword, and I think I have to somehow do something similar to sprite flipping or something? I guess?

here’s my sword node hierarchy for reference

and here’s the animation player if it helps in any way…

and just to be safe here’s the script attached to sword:

extends Node2D

@export var stats: sword_properties

var attacking = false

func _physics_process(delta: float) → void:
if !attacking:
look_at(get_global_mouse_position())
handle_input()

func swing():
attacking = true
$AnimationPlayer.play(“swing”)
$PivotPoint/Sprite2D/Area2D/WeaponHitBox.disabled = false

func thrust():
attacking = true
$AnimationPlayer.play(“thrust”)
$PivotPoint/Sprite2D/Area2D/WeaponHitBox.disabled = false

func handle_input():
if Input.is_action_just_released(“thrust_attack”):
thrust()
if Input.is_action_just_released(“swing_attack”):
swing()

func _on_animation_player_animation_finished(anim_name: StringName) → void:
attacking = false
$PivotPoint/Sprite2D/Area2D/WeaponHitBox.disabled = true

func _on_area_2d_body_entered(body: Node2D) → void:
if body.is_in_group(“enemy”):
body.take_damage(stats.thrust_attack_damage)

I know this might be a simple issue but It’s been bugging me for a while now… any help would be appreciated, thank you

Make two versions of the animation and choose which one will play depending on sword’s current rotation angle.

1 Like

Hi after working on it and scratching my head for a while I managed to finally do it and in the end it was simpler than I expected, thank you :smiley: