Topdown 2D - Adding rotation to 4 direction weapon animation

Godot Version

4.3

Question

I have started learning Godot just last weekend, but I have run into a problem that I haven’t been able to resolve in the past 2 days.

Hopefully I don’t butcher explaining my problem, but here it goes:

I am trying to get my Sprite2D(Weapon) to point at my mouse direction whenever I press attack. I have a AnimationPlayer along with a AnimationTree setup for attack animations in 4 directions, and I want the weapon animation to point exactly towards the direction of my mouse when I fire.

For example, If my cursor is top left of the weapon, I want the attack_up animation to play, while rotating so it faces towards the mouse at the time of playing animation. I want the same thing to happen if I put my mouse to the left of weapon, but to play attack_left animation while having it rotate so it faces towards the mouse.

I have been looking around the AnimationPlayer Node, and I happened to find a track I could add alongside my frame and texture in Sprite2D called “rotation,” so I tried inserting a key at the start but I have no idea how to call the rotation track inside my AnimationPlayer in my script in order to change the degrees/radian of the specific animation.


any help on the scripting would be greatly appreciated!

Hm there should be an easy solution for your problem, im not sure about your whole setup though.
These keys are normally not supposed to be changed in code, you can click on the key an then change the value in the inspector, but you could also just rotate in code:

look_at(get_global_mouse_position)