Help with player animation player walk in a idle state

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Link901

Hi, I need some help with my player animation so far everything was good until I add an idle animation and when I press any direction to walk the player won’t change animation it’s stuck in an idle frame.

Code of the animation below

#Player Animation 

if (motion.length() > Speed_Idel*0.09):
	if (Input.is_action_pressed("ui_up")):
		Anim = "Walk_U"
	
if (Input.is_action_pressed("ui_down")):
		Anim = "walk_D"

if (Input.is_action_pressed("ui_left")):
	Anim = "walk_L"
	
if (Input.is_action_pressed("ui_right")):
		Anim = "walk_R"

# problem starts here the animation won't change back to walking just idle 
#Player walk in a idle state
else:
	if (RayNode.get_rotation_degrees() == 180):
		Anim = "Idle _U"

if (RayNode.get_rotation_degrees() == 0):
		Anim = "Idle _D"

if (RayNode.get_rotation_degrees() ==-90):
		Anim = "Idle _L"
	
if (RayNode.get_rotation_degrees() ==90):
		Anim = "Idle _R"

if Anim != AnimNew:
	AnimNew = Anim
	PlayerAnimNode.play(Anim)

Is there a situation where you are pressing in a certain direction (left, for instance), and the function RayNode.get_rotation_degrees() keeps returning 180, 90, or some other value? Maybe look at what value is returned by RayNode.get_rotation_degrees() (i.e. turn on debugging, or use some breakpoints).

Ertain | 2019-06-23 03:47

:bust_in_silhouette: Reply From: Dlean Jeans

You probably need to put the idle part of the code before the walk part. And you can use match to shorten the idle part:

match RayNode.rotation_degrees:
	180:
		Anim = "Idle_U"
	0:
		Anim = "Idle_D"
	-90:
		Anim = "Idle_L"
	90:
		Anim = "Idle_R"

# there's no need for parentheses ( ) around if boolean statements
if motion.length() > Speed_Idel*0.09:
	if Input.is_action_pressed("ui_up"):
		Anim = "Walk_U"
	elif Input.is_action_pressed("ui_down"):
		Anim = "walk_D"
	elif Input.is_action_pressed("ui_left"):
		Anim = "walk_L"
	elif Input.is_action_pressed("ui_right"):
		Anim = "walk_R"

if Anim != AnimNew:
    AnimNew = Anim
    PlayerAnimNode.play(Anim)