I wrote a code to move the player with wasd but I wanted to change the animation based on where the mouse is pointed to, but when I wrote a function to do that, it only worked for the down and left animations, and when the character moves, the animations stop changing at all.
I used the "get_angle_to()" function and used some if statements to select which animation should be played, the code I used to do this is as follows
extends CharacterBody2D
var speed = 300.0
var player_is_walking
func _physics_process(delta):
var direction = Input.get_vector("left","right","up","down")
if direction.x == 0 and direction.y == 0:
player_is_walking = false
elif direction.x != 0 or direction.y != 0:
player_is_walking = true
velocity = direction * speed
move_and_slide()
play_anim()
func play_anim():
var angle = get_angle_to(Vector2.ZERO.direction_to(get_local_mouse_position()))
if player_is_walking:
if angle > 5.49779 and angle <= 7.06858:
$AnimatedSprite2D.play("U_Walking")
if angle > 0.785398 and angle <= 2.35619:
$AnimatedSprite2D.play("D-Walking")
if angle > 2.35619 and angle <= 3.92699:
$AnimatedSprite2D.play("R-Walking")
if angle > 3.92699 and angle <= 5.49779:
$AnimatedSprite2D.play("L-Walking")
else:
if angle > 5.49779 and angle <= 7.06858:
$AnimatedSprite2D.play("U_Idle")
if angle > 0.785398 and angle <= 2.35619:
$AnimatedSprite2D.play("D-Idle")
if angle > 2.35619 and angle <= 3.92699:
$AnimatedSprite2D.play("L-Idle")
if angle > 3.92699 and angle <= 5.49779:
$AnimatedSprite2D.play("R-Idle")